开发者

Entity framework and pure POCO update

How do I do an update on a pure POCO object using entity framework 4?

Lets say I change the person's first name and call the repository in this manner:

    public User Update(User user)
    {
   开发者_运维知识库     //User originalUser = GetUser(user.UserId);

        //Is there a way to update the values that are only changed?

        context.Users.Attach(user);
        context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
        return user;
    }

I dont want null values to update the database to null. For eg. Suppose I have LastName as a property but when passing the object to the update function, it was null. Am I going to have to get the originalUser and then update each property accordingly?


       "update each property accordingly?"

No, you can use,

       context.ObjectStateManager.TryGetObjectStateEntry(newItem, out entity);

      // this will gives you the entity present in db and after that I suggest to write your code to change the state and save.

Also suggest you to read this for more info on tracking changes in POCO entities

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜