实体框架的错误:使用空的EntityKey值的对象不能附加到对象上下文 [英] Entity Framework Error: An object with a null EntityKey value cannot be attached to an object context

查看:783
本文介绍了实体框架的错误:使用空的EntityKey值的对象不能附加到对象上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序有以下代码...

In my application I have the following code...

    public Boolean SaveUserInformation(UserInfoDTO UserInformation)
    {
        return dataManager.SaveUserInfo(new UserInfo()
        {
            UserInfoID = UserInformation.UserInfoID.HasValue ? UserInformation.UserInfoID.Value : 0,
            UserID = UserInformation.UserID,
            ProxyUsername = UserInformation.ProxyUsername,
            Email = UserInformation.Email,
            Status = UserInformation.Status
        });
    }

这代码调用,利用实体框架DATAMANAGER对象的方法...

This code calls a method on a dataManager object that utilizes Entity Framework...

    public Boolean SaveUserInfo(UserInfo userInfo)
    {
        try
        {
            //Validate data prior to database update
            if (userInfo.UserID == null) { throw new Exception("UserInfoDomainModel object passed to PriorityOne.Data.DataManager.SaveUserInfo with UserID property set to NULL."); }
            if (userInfo.ProxyUsername == null) { throw new Exception("UserInfoDomainModel object passed to PriorityOne.Data.DataManager.SaveUserInfo with ProxyUsername property set to NULL."); }
            if (userInfo.Email == null) { throw new Exception("UserInfoDomainModel object passed to PriorityOne.Data.DataManager.SaveUserInfo with Email property set to NULL."); }

            if (userInfo.UserInfoID == 0)
            {
                //Perform Insert
                using (PriorityOneEntities entities = new PriorityOneEntities())
                {
                    entities.UserInfoes.AddObject(userInfo);
                    entities.SaveChanges();
                }
            }
            else
            {
                //Perform Update
                using (PriorityOneEntities entities = new PriorityOneEntities())
                {
                    entities.Attach(userInfo);
                    entities.SaveChanges();
                }
            }

            return true;
        }
        catch (Exception ex)
        {
            //TODO: Log Error
            return false;
        }

    }

在此代码中插入工作得精细。但是,当我尝试执行更新我收到一个错误说:有一个空的EntityKey值的对象不能附加到对象上下文

The insert on this code works just fine. But when I try to perform an update I'm getting an error saying: "An object with a null EntityKey value cannot be attached to an object context."

它发生在这行代码:entities.Attach(用户信息);

It occurs on this line of code: entities.Attach(userInfo);

我试图做到的是,以避免往返到数据库中只选择记录我将在稍后进行修改和更新,从而使两个来回到数据库中。

What I'm trying to accomplish is to avoid making a round trip to the database just to select the record that I will later make changes to and update, thus making two round trips to the database.

任何想法了什么错误,或者我如何能更好地做到这一点?

Any ideas what is going wrong, or how I could better accomplish this?

感谢。

推荐答案

好像你正在使用EF 4.1+ 结果
你必须告诉你希望你的实体进行更新EF(修改状态):

Seems like you're using EF 4.1+
You have to tell EF that you want your entity to be updated (Modified state):

//Perform Update
using (PriorityOneEntities entities = new PriorityOneEntities())
{
    entities.Entry(userInfo).State = EntityState.Modified;
    entities.SaveChanges();
}



P.S。您不必显式调用连接。它的引擎盖下完成的。

P.S. You don't have to explicitly call Attach. It's done under the hood.

更新:根据您的意见结果
,你使用EF 4.0。下面是你必须做附加你的对象在EF 4.0修改:

Update:
based on your comments, you're using EF 4.0. Here's what you have to do to attach your object as modified in EF 4.0:

 ctx.AddObject("userInfoes", userInfo);
 ctx.ObjectStateManager.ChangeObjectState(userInfo, EntityState.Modified);
 ctx.SaveChanges();  

您不能使用连接方法。从 http://msdn.microsoft.com/en-us/library/bb896271.aspx

如果一个特定类型的多个实体有相同的键值,实体框架将抛出​​一个异常。要避免遇到异常,使用 ADDOBJECT 方式附着分离的对象,然后相应地更改状态。

If more than one entity of a particular type has the same key value, the Entity Framework will throw an exception. To avoid getting the exception, use the AddObject method to attach the detached objects and then change the state appropriately.

这篇关于实体框架的错误:使用空的EntityKey值的对象不能附加到对象上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆