many to many - EF CodeFist Add rather than Update -


i have 2 classes, 1 post , other category, many-to-many relationship.

when create new post business object, , category object.

post.categories.add(category) 

then using automapper map post business model object data entity, when

dbcontext.posts.add(post);  dbcontext.savechanges(); 

it add new row categories table, category instance has value id, key in database. 1 knows how change this? if category info exists in database, nothing, except add new row categpryposts table

you need attach category context:

dbcontext.categories.attach(category); 

that tells ef it's existing, unmodified entity.


Comments