实体框架保存集合

本文关键字:集合 保存 框架 实体 | 更新日期: 2023-09-27 18:02:23

我有实体框架代码优先模型。当我尝试在一次插入中保存帐户集合时,我会收到错误消息。

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

插入新的用户设备

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();

错误消息

{"无法将值NULL插入表'LICENSE_WATCHER_TEST.dbo.Accounts'的列'Acc_Id';列不允许为NULL。insert失败。''r''n语句已终止。"}

当我尝试保存Accout系列时,就会发生这种情况。有没有办法将Accouts集合保存在一个Insert中?

实体框架保存集合

Acc_id在插入时为null。在这种情况下,我希望ID是自动生成的。是否有可能将账户表上的Acc_id列设置为PK,而不将数据库中的Identity列设置为?

这可能会导致你正在经历的行为。

好吧,您已经将Acc_Id定义为Primay键,并且似乎正在尝试向表中插入一个null值。

您在这里创建帐户列表,但只填写Name

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

然后你不在accountsList中做任何更改在提交之前,您要执行没有填写IdAccounts = accountsList,然后尝试提交。

试试这样的东西:

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