该属性是对象的关键信息的一部分,无法修改

时间:2015-07-31 10:28:43

标签: c# asp.net-mvc entity-framework entity-framework-6

我有以下模型类,其NETWORKID(PK)不是系统生成的: -

  public partial class NetworkInfo
    {
        public long NETWORKID { get; set; }
        public long WORKSTATIONID { get; set; }
        public string NICNAME { get; set; }
        public string NICDESCRIPTION { get; set; }
        public string IPADDRESS { get; set; }
        public string MACADDRESS { get; set; }
        public string IPNETMASK { get; set; }
        public string NETWORK { get; set; }
        public string GATEWAY { get; set; }
        public string ISDHCP { get; set; }
        public string DHCPSERVER { get; set; }
        public string NICLEASE { get; set; }

        public virtual Resource Resource { get; set; }
    }
}

现在我将当前的networkinfo实体保存在列表中,然后我想再次添加它们,方法是为它们分配新的NetworkID。所以我尝试以下方法: -

public void SaveAdditionalNeyworkInfo(long assetid, List<NetworkInfo> networkinfo) 
        {
            int i = 1;
            long max = entities.NetworkInfoes.Max(a => a.NETWORKID);
            foreach (var e in networkinfo.OrderBy(a=>a.NETWORKID))
            {
                if (i == 1)
                    i++;
                else
                {
                    e.NETWORKID = max + 1;
                    entities.NetworkInfoes.Add(e);
                    max++;
                }

            }

        }

但这会引发以下错误: -

The property 'NETWORKID' is part of the object's key information and cannot be modified.

但如果我按照以下方式重新编写它,那么我可以添加新的实体: -

public void SaveAdditionalNeyworkInfo(long assetid, List<NetworkInfo> networkinfo) 
        {
            int i = 1;
            long max = entities.NetworkInfoes.Max(a => a.NETWORKID);
            foreach (var e in networkinfo.OrderBy(a=>a.NETWORKID))
            {
                if (i == 1)
                    i++;
                else
                {
                   NetworkInfo n = new NetworkInfo()
                   {
                       NETWORKID = max+1,
                       WORKSTATIONID = assetid,
                       NICNAME =e.NICNAME,
                        NICDESCRIPTION = e.NICDESCRIPTION,
                      IPADDRESS = e.IPADDRESS,
                        MACADDRESS=e.MACADDRESS,
                       IPNETMASK=e.IPNETMASK,
                        NETWORK=e.NETWORK,
                        GATEWAY=e.GATEWAY,
                       ISDHCP=e.ISDHCP,
                       DHCPSERVER=e.DHCPSERVER,
                       NICLEASE=e.NICLEASE

                   };

                 entities.NetworkInfoes.Add(n);
                   max++;
                }

            }

所以我的问题是为什么在第一种方法中我得到一个错误,即NetworkID无法修改,而在第二种方法中我没有得到任何错误?虽然在这两种情况下,我在我的代码中分配了NetworkID?

1 个答案:

答案 0 :(得分:1)

我猜您首先从EF Context(List networkinfo函数参数)中读取记录,然后尝试更新主键。当你阅读它们时,EF会跟踪它们进行修改并在SaveChages上更新它们(尽管你调用了Add方法暗示插入)。所以对数据库的请求可能是:

UPDATE NetworkInfo
SET NETWORKID = 5
WHERE NETWORKID = 1

但EF不允许更改主键及其原因,如here in comments by KM所述:

  

可能是不能/不想处理级联更新   所有FK

所以你在第一种情况下得到例外。
在第二种情况下,新对象来到EF,并且通常的插入将被发送到数据库服务器。