开发者

Entity Framework 4, POCO, WCF, Updating Many-To-Many

开发者 https://www.devze.com 2023-02-04 11:36 出处:网络
I\'ve got a many-to-many relationship mapped in my Entity Framework POCO classes. In the database it\'s a join table with a composite key, and the POCO properties are generated fine.

I've got a many-to-many relationship mapped in my Entity Framework POCO classes. In the database it's a join table with a composite key, and the POCO properties are generated fine.

When I load an entity from the context, I can remove an item from the many-to-many collection and the database is updated when I save changes.

For Examp开发者_如何学Cle:

var item = context.Items.First();

item.OtherItems.Remove(item.OtherItems[0]);

context.SaveChanges();

However, when the detached object graph comes back from WCF, I attach it to the context and mark it as modified. But the changes are not persisted.

Example:

// this happens on the silverlight client

item.OtherItems.Remove(item.OtherItems[0]);

// and on the server

context.Items.Attach(item);

context.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);

context.SaveChanges();

In this case the record is not removed from the join table in the database. Any ideas how I can get this to work? Thanks very much in advance.


Changing object state marks your entity modified. You need to use ChangeRelationshipState to mark modified relation between two entities - this will perform DB modification on your join table. You will need to set relation's state as Added or Deleted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号