繁体   English   中英

实体框架保存集合

[英]Entity Framework save 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();

错误信息

{“无法将值NULL插入表'LICENSE_WATCHER_TEST.dbo.Accounts'的列'Acc_Id'中;该列不允许为空。INSERT失败。\\ r \\ n该语句已终止。”}

当我尝试保存Accout集合时发生。 有没有一种方法可以将Accouts集合保存在一个Insert中?

Acc_id在插入时为null。 我希望在这种情况下可以自动生成ID。 是否有可能在“帐户表”上将“ Acc_id”列设置为PK,而在数据库中却没有将“身份”列设置为?

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

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

您在此处创建帐户列表,但仅填写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 });
        }  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM