When I update this entity I get this error:
My Class for database is different with my class in viewmodel
but I convert it.
Please help me and send me the correct code.
Thanks
My error:
Attaching an entity of type 'DomainModel.Models.Tbl_Images' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate
My code:
public bool Update(ImagesEditVM model)
{
bool result = false;
try
{
DomainModel.Models.Tbl_Images img = new Tbl_Images
{
Id = model.Id,
Code = model.Code,
Image = model.Image,
Language = model.Language,
Title = model.Title
};
db.Tbl_Images.Attach(img);
db.Entry<DomainModel.Models.Tbl_Images>(img).State = EntityState.Modified;
db.SaveChanges();
result = true;
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}