How to ignore properties in Entity Framework 6 for Update?

c# entity-framework entity-framework-6

Question

I am currently trying to update using Entity Framework 6 and am having some trouble ginven a PK property in my DB, which I DONT want to edit but cant find the correct way to ignore it.

myobj aux = new obj(){
//code
};

using (var context = new ModelEntity())
{
    var item = context.mytable.Find(id);
     context.Entry(item).CurrentValues.SetValues(aux);
     context.SaveChanges();
}

I get the error: The property 'xxxx' is part of the object's key information and cannot be modified. '

I have searched for some kind of ignore and have found nothing. All I find are EF4 and 5.

1
0
8/29/2018 12:47:29 PM

Accepted Answer

If you are trying to ignore properties in an entity, then you would need to add a [NotMapped] attribute to that property in the class. More documentation here: https://docs.microsoft.com/en-us/ef/core/modeling/included-properties

Also, it seems like the error presented is more to do with you attempting modify a Primary key or index. I see this is actually answered here: https://stackoverflow.com/a/12397981/1670574

2
8/29/2018 12:56:57 PM


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