John John
John John

Reputation: 1

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

I have the following model class, where its NETWORKID (PK) is not system generated:-

  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; }
    }
}

now i am saving the current networkinfo entities inside a list, then i want to add them again , by assigning new NetworkID to them. so i try the following :-

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++;
                }

            }

        }

but this raise the following error:-

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

but if i re-write it as follow then i can add new entities :-

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++;
                }

            }

so my question is why in the first approach i got an error that the NetworkID can not be modified , while in the second approach i did not get any error ? although in the two cases i am assign the NetworkID inside my code ?

Upvotes: 0

Views: 10208

Answers (1)

FireAlkazar
FireAlkazar

Reputation: 1880

I guess you first read records from EF Context(List networkinfo function argument) and then try to update primary keys. As you read them, EF tracks them for modifications and will update them on SaveChages(despite you called Add method implying to insert). So the request to database could be like:

UPDATE NetworkInfo
SET NETWORKID = 5
WHERE NETWORKID = 1

But EF does not allow to change the primary key and the reason probably as described here in comments by KM:

it might be that is can't/doesn't want to handle cascading the update to all FKs

So you get exception in first case.
In second case new object comes to EF, and a usual insert will be send to database server.

Upvotes: 1

Related Questions