How to update entity?

痞子三分冷 提交于 2019-12-20 05:48:05

问题


I had a question more detailed earlier which I had no answer, I will have the same question with a simpler way:

I have an EF database with foreign key to another table.

I would like to UPDATE an ENTITY. But I need to this like this and I'll write the codes below:

  1. Go to database and retrieve the Member by id, return EF Member object
  2. Do some changes on the object OUTSIDE the EF Context
  3. Send the MODIFED EF Member into a Save method
  4. In BL layer save method uses the context and save changes.

1)

MemberManager currentMemberManager = new MemberManager();
Member NewMember = currentMemberManager.GetById(2);

2)

NewMember.FirstName = "NewFirstName";
NewMember.LanguageId = 1;

3)

currentMemberManager.Save(NewMember);

4)

public void Save2(Member newMember)
{
    using (var Context = new NoxonEntities())
    {
        Member existingMember = Context.Member.First(c => c.Id == newMember.Id);
        existingMember.FirstName = newMember.FirstName;
        existingMember.Language = Context.Language.First(c => c.Id == newMember.LanguageId);
        Context.SaveChanges();//In here I get the error below
    }
}

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

Note: You may suggest to SEND a different class (Ex: public class MyMember) that has all the necessary properties and totally separated from EF. But this requires much work to get all EF object converting into my separate classes. Am I right?

I am hoping there is a way to Detach the entity just long enough for me to modify it and save the values into database. (Also, I tried the Detach method which updates no rows at all)

I've been trying to solve this for hours now.

Please, help me to understand it better, I really need a solution. Thank you so much to anyone how has some ideas.


回答1:


Could you do something simple like detaching the entity, then attaching it to the context when you're ready to save?

MemberManager currentMemberManager = new MemberManager();
Member NewMember = currentMemberManager.GetById(2);

The get:

public Member GetById(int id)
{ 
  var member = YourContext.Members.FirstOrDefault(m => m.id == id);
  YourContext.Detach(member);
  return member;
}

The save:

public void Save2(Member newMember)
        {
            using (var Context = new NoxonEntities())
            {
                Context.Attach(newMember);
                Context.ObjectStateManager.ChangeObjectState(newMember, EntityState.Modified);
                Context.SaveChanges();
            }
        }


来源:https://stackoverflow.com/questions/12389704/how-to-update-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!