Simple Entity Framework copy records operation, or so I thought?

  • Thread starter Thread starter DD
  • Start date Start date
D

DD

Begginig EF, and one simple operation I am trying to do...yet I can not do it
simply.

I have 2 DB on 2 different servers, trying to copy records from one table
(which has no references to other tables in the EF model) to the same table
in the second DB/second server.

What I can't figure out is how to do it (since there is no deep
copy/cloning)...this doesn't work:


Entities sourceContext = new
Entities(ConfigurationManager.ConnectionStrings["SourceEntities"].ConnectionString);
Entities destinationContext = new
Entities(ConfigurationManager.ConnectionStrings["DestinationEntities"].ConnectionString);

IQueryable<TABLE1> tmpRecords = from records in sourceContext.TABLE1
select records;
List<TABLE1> recordList = tmpRecords.ToList();
foreach (TABLE1 record in recordsList)
destinationContext.AddToTABLE1(record);


Exception thrown is "The object cannot be added to the ObjectStateManager
because it already has an EntityKey. Use ObjectContext.Attach to attach an
object that has an existing key."

So my question is, what to do for this code to work? Or how to do it in
another simple way please?

Thanks in advance
 
Unfortunatelly seem it's just as I've suspected it...there is no easy way to
do what I'm trying to do.
The sample code in figure 7 implies that to copy an entity to a new instance
of the entity object withe the same (entity) type you need to go though each
and every attribute and copy them manually (which is fine for object with 3
attributes, but what if you have 33???).


Am I wrong? Please correct me if I am, I really would like that, reallllly :)

SAMPLE CODE THAT IMPLIES WHAT I THINK
// Add order details
foreach (DTO.OrderDetail od in order.OrderDetails)
{
Order_Detail detail = new Order_Detail
{
Order = nwOrder,
Product = (from p in db.ProductSet
where p.ProductID == od.ProductID
select p).First(),
Quantity = od.Quantity,
UnitPrice = od.UnitPrice,
Updated = od.Updated
};
nwOrder.Order_Details.Add(detail);
}


Cowboy (Gregory A. Beamer) said:
I may have spoken too soon. Looks like there is an MSDN article that might
help you out in your quest:
http://msdn.microsoft.com/en-us/magazine/dd263098.aspx

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*************************************************
| Think outside the box! |
*************************************************

DD said:
Begginig EF, and one simple operation I am trying to do...yet I can not do
it
simply.

I have 2 DB on 2 different servers, trying to copy records from one table
(which has no references to other tables in the EF model) to the same
table
in the second DB/second server.

What I can't figure out is how to do it (since there is no deep
copy/cloning)...this doesn't work:


Entities sourceContext = new
Entities(ConfigurationManager.ConnectionStrings["SourceEntities"].ConnectionString);
Entities destinationContext = new
Entities(ConfigurationManager.ConnectionStrings["DestinationEntities"].ConnectionString);

IQueryable<TABLE1> tmpRecords = from records in sourceContext.TABLE1
select
records;
List<TABLE1> recordList = tmpRecords.ToList();
foreach (TABLE1 record in recordsList)
destinationContext.AddToTABLE1(record);


Exception thrown is "The object cannot be added to the ObjectStateManager
because it already has an EntityKey. Use ObjectContext.Attach to attach an
object that has an existing key."

So my question is, what to do for this code to work? Or how to do it in
another simple way please?

Thanks in advance
 
I could be wrong, but do you need to Detatch the entities from the first
context before you can add them to the second? When you attempt to add them
to destinationContext the entities still think they belong to sourceContext.
Try sourceContext.Detatch(entity);

Trevor
 
Back
Top