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 ?