Entity Framework 코드 첫 번째 모델이 있습니다. 한 컬렉션에 계정 컬렉션을 저장하려고하면 오류 메시지가 나타납니다. 새로운 UserDevice의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; }
}
삽입
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();
이
{ "열에 'Acc_Id', 테이블 'LICENSE_WATCHER_TEST.dbo.Accounts'NULL 값을 삽입 할 수 없습니다 오류 메시지, 열 않습니다 null을 허용하지 않습니다. INSERT가 실패합니다. \ r \ n 내용이 종료되었습니다. "}
그것은 내가 Accout 컬렉션을 저장하려고 할 때 일어난다. 하나의 삽입물에 Accouts 컬렉션을 저장하는 방법이 있습니까?
ID는 실제로 데이터베이스에 설정되지 않았습니다. 문제는 유창한 API 구성에있었습니다. – Michal