简体   繁体   中英

How to ignore properties in Entity Framework 6 for Update?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM