Zealot91
Zealot91

Reputation: 155

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.

Upvotes: 1

Views: 4279

Answers (1)

Dave Agaba
Dave Agaba

Reputation: 213

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://learn.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

Upvotes: 3

Related Questions