2016-08-12 80 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 }); 
     }