c# - Using Entity Framework 4.1, how can I achive below requirement? -
consider have 2 entities - a) publisher b) book publisher has navigation property called publishedbooks collection of books. assume publisher1 has published 2 books ie book1 & book2 is, publisher1, delete published book book1 , add new published book (ie book3), in database.
context.savechanges() throwing below error -
the operation failed: relationship not changed because 1 or more of foreign-key properties non-nullable. when change made relationship, related foreign-key property set null value. if foreign-key not support null values, new relationship must defined, foreign-key property must assigned non-null value, or unrelated object must deleted.
note : delete cascade rule present in database in context class. btw, using c# & sql server 2005.
sounds doing this:
foo foo = entity.relatedfoos.where(f=>f.id=xyz); entity.relatedfoos.remove(foo); //this problematic line context.savechanges(); that is; removing entity set of related items - not main collection on context itself. noted in error, removing above way removes relation. not object foo. meant, if mean delete foo:
foo foo = entity.relatedfoos.where(f=>f.id=xyz); context.foos.remove(foo); context.savechanges();
Comments
Post a Comment