This is a migrated thread and some comments may be shown as answers.

"clean" an ORM object

1 Answer 71 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Thomas
Top achievements
Rank 1
Thomas asked on 23 Feb 2012, 11:23 AM
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
-Thomas

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 27 Feb 2012, 12:26 PM
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,
Alexander
the Telerik team
Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
Tags
Development (API, general questions)
Asked by
Thomas
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or