Set collection to modified Entity Framework

c# entity-framework-6

Question

How can I modify a collection the same way I would modify a variable?

_context.Entry(thing).Property(x => x.MyProperty).isModified = true;

like:

_context.Entry(thing).Collection(x => x.MyCollection).isModified = true;

EDIT: This is for my collection, which is a list of items kept in a lookup table. I want to update the relationships in this collection, which will simply contain a list of stubs with their ids, without affecting the audit values or anything else found in the lookup objects. For instance, a contact will have many contact kinds, which are complex objects in this situation for whatever reason. I want to be able to add and remove types solely using FKs, leaving relationship fixups up to EF.

public class Contact
{
   public int Id {get;set;}
   public list<ContactTypes> ContactTypes {get;set;}
   //audit values/other properties
}

public class ContactType
{
   public int Id {get;set;}
   public string Value {get;set;}
}
1
4
2/4/2019 11:44:41 PM

Accepted Answer

ZZZ_tmp
3
6/5/2015 2:47:54 PM

Popular Answer

You probably already know how annoying it is to force EF's Relationship Fixup on a list of ForeignKey objects. Here's a clever approach to achieve that.

public void SetContactTypesToUnchanged(Contact contact)
  {
    contact.ContactTypes.Each(type => _context.Entry(type).State = EntityState.Unchanged);
    _context.SaveChanges();
  }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow