I have Entity Framework Code First models. When I try save collection of Accounts in one insert i get error message.
public class User
{
[Key]
public int Usr_Id { get; set; }
[Required]
[MaxLength(35)]
public string Name { get; set; }
[Required]
[MaxLength(35)]
public string Surname { get; set; }
[Required]
[MaxLength(35)]
public string Location { get; set; }
//NAVIGATION
public User()
{
UserDevices = new List<UserDevice>();
}
public virtual ICollection<UserDevice> UserDevices { get; set; }
}
public class UserDevice
{
[Key]
public int UsrDev_Id { get; set; }
[Required]
[MaxLength(50)]
public string PurposeOfUse { get; set; }
// NAVIGATION
public UserDevice()
{
Accounts = new List<Account>();
}
//User can have many UserDevice
public int Usr_Id { get; set; }
public virtual User User { get; set; }
//UserDevice can have many Acoount
public virtual ICollection<Account> Accounts { get; set; }
//One to one
public virtual SoftwareInformation SoftwareInformation { get; set; }
public virtual HardwareInformation HardwareInformation { get; set; }
}
public class Account
{
[Key]
public int Acc_Id { get; set; }
public string Name { get; set; }
//NAVIGATION
//UserDevice can have many Account
public int UsrDev_Id { get; set; }
public virtual UserDevice UserDevice { get; set; }
}
Insert of new UserDevice
List<Account> accountsList = new List<Account>();
foreach (var item in model.DeviceInformations.OS.Accounts)
{
accountsList.Add(new Account { Name = item.Name });
}
_unitOfWork.UserDevices.Add(new UserDevice
{
Usr_Id = model.Usr_Id,
PurposeOfUse = model.PurposeOfUse,
HardwareInformation = new HardwareInformation
{
MB_Manufacturer = model.DeviceInformations.Motherboard.Manufacturer,
MB_Model = model.DeviceInformations.Motherboard.Model,
MB_Name = model.DeviceInformations.Motherboard.Name,
MB_UUID = model.DeviceInformations.Motherboard.UUID,
CPU_Manufacturer = model.DeviceInformations.Processor.Manufacturer,
CPU_MaxClockSpeed = model.DeviceInformations.Processor.MaxClockSpeed,
CPU_Name = model.DeviceInformations.Processor.Name,
},
SoftwareInformation = new SoftwareInformation
{
OS_Edition = model.DeviceInformations.OS.Edition,
OS_HostName = model.DeviceInformations.OS.HostName,
OS_Language = model.DeviceInformations.OS.Language,
OS_Platform = model.DeviceInformations.OS.Platform,
OS_ProductKey = model.DeviceInformations.OS.ProductKey,
OS_ServicePackVersion = model.DeviceInformations.OS.ServicePackVersion,
OS_Version = model.DeviceInformations.OS.Version
},
Accounts = accountsList
});
_unitOfWork.Commit();
Error message
{"Cannot insert the value NULL into column 'Acc_Id', table 'LICENSE_WATCHER_TEST.dbo.Accounts'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
It happends when I try save Accout collection. Is there a way how to save Accouts collection in one Insert?
Acc_id is null on insert. I would expect that ID to be auto generated in cases like this. Is it possible that you have the Acc_id column on the Account Table set up as PK, but not an Identity column in the database?
That might cause the behavior that you are experiencing.