实体框架保存集合

时间:2016-08-12 20:21:19

标签: c# entity-framework-4

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

 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插入列'Acc_Id',表'LICENSE_WATCHER_TEST.dbo.Accounts';列不允许空值.INSERT失败。\ r \ n语句已终止。”}

当我尝试保存Accout集合时会发生这种情况。有没有办法如何在一个插入中保存Accouts集合?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

好吧,您已将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 });
        }