"clean" an ORM object

Thread is closed for posting
2 posts, 0 answers
  1. Thomas
    Thomas avatar
    4 posts
    Member since:
    Jun 2011

    Posted 23 Feb 2012 Link to this post

    I have got two DAL connection, one to an mssql database, the other to an compact 4.0 database. Both databases are created using the same EntityDiagram.

    Next I want to sync the new data from the mssql database, into the compact.
    I tried using an approach like 'compactconnection.Add(mssqlconnection.DALCities.ToList())' but I kept getting an 'instances failed to persist' exception.

    Code to get the DALCities list:
     "return connection.DALCities.ToList();"

    When I use another approach like:
    - select all DALCity records from mssql database into list1
    - loop list1, create new DALCity object, manually set values of .Name and .CountryId, add to list2
    - add list2 to compact database

    New code to get the new list:
    "return connection.DALCities.Select(item => new DALCity { CityId = item.CityId, CityName = item.CityName}).ToList();"

    That works fine, so my guess is that some of the metadata in the DALCity that I have selected from the mssql database keeps telling the compact database that this is an illegal object.

    So my question is if there is an easier, more automagical way to 'clean' the objects by removing all metadata, so that the compact database will receive the object. That would save me a lot of coding (there are many tables), and reduces the chance of forgetting a field of two.

    hope this made some kind of sense :)

    Thanks in advance
  2. Alexander
    Alexander avatar
    727 posts

    Posted 27 Feb 2012 Link to this post

    Hello Thomas,

    An OpenAccess persistent object can be managed by only one context at the same time. That is why you are getting those errors - the objects retrieved from the first context are bound to it and adding them to the second context is not allowed.
    This could work if the objects are "detached" from the first context but unfortunately there is no such functionality in the OpenAccess' API, it is still on our TODO list. A possible solution at the moment is to serialize (and then deserialize) the objects to a memory stream, this way they will loose their context information. However, this approach seems harder to implement and more error-prone than the one you are currently using.

    All the best,
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
Back to Top