2016-08-12 82 views
0

我有实体框架代码第一个模型。当我尝试保存收集帐户在一个插入我收到错误消息。实体框架保存集合

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';柱确实\ r \ n声明已被终止。“}

当我尝试保存Accout集合时发生。有没有办法如何在一个Insert中保存Accouts集合?

回答

1

ACC_ID是插入空。我期望在这种情况下自动生成ID。是否有可能将Account Table上的Acc_id列设置为PK,但不是数据库中的Identity列?

这可能会导致您遇到的问题。

+1

身份确实没有在数据库中设置。问题出在流畅的api配置上。 – Michal

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