Entity Framework 保存 collection

Entity Framework save collection

我有 Entity Framework 个 Code First 模型。当我尝试在一个插入中保存 collection 个帐户时,我收到错误消息。

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

}

插入新的 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();

错误信息

{"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."}

当我尝试保存帐户 collection 时发生了。有没有办法在一次插入中保存帐户 collection?

好吧,您已将 Acc_Id 定义为 Primay 键,您似乎正试图向 table 插入一个空值。

此处创建账户列表,但只填写Name

        foreach (var item in model.DeviceInformations.OS.Accounts)
        {
            accountsList.Add(new Account { Name = item.Name });
        }    

然后您不对 accountsList 进行任何更改 在提交之前你做了 Accounts = accountsList 没有填写 Id 然后你尝试提交。

尝试这样的事情:

        foreach (var item in model.DeviceInformations.OS.Accounts)
        {
            accountsList.Add(new Account { Acc_Id = !!someIdHere!!, Name = item.Name });
        }  

Acc_id 在插入时为空。我希望在这种情况下自动生成该 ID。是否有可能您将帐户 Table 上的 Acc_id 列设置为 PK,而不是数据库中的标识列?

这可能会导致您遇到的行为。