[英]Add multiple relational data into SQL server using EF core
我正在嘗試使用 EF 核心 5 將多個關系數據插入到 SQL 服務器數據庫中。我想插入多個父級和多個子級。 父母和孩子是一對多的關系。 我正在嘗試使用 context.AddRang(lstparents) 添加它它僅為一個父母插入子實體數據,而對於其他父母則沒有條目。 你能幫我解決這個問題嗎?
我的模特
Public class Parent
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
Public int64 Id {get; set;}// this is identity column
Public string Name { get; set;}
Public List<Child> child { get; set;}
}
Public class Child
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
Public int64 id { get; set;} // identity column
Public string ChildName { get; set;}
Public int64 ParentId { get; set;}
[Foreign key("ParentId")]
Public Parent parent { get; set;}
}
// Here is insertion logic
Public void main(string[] args)
{
List<Child> lstChild = new List<Child> ()
{
new Child{ ChildName= "Child1Name"},
new Child{ ChildName= "Child2Name"},
new Child{ ChildName= "Child3Name"}
};
List<Parent> lstparents = new List<Parent>()
{
new Parent {Name = "xyz", Child= lstChild },
new Parent {Name = "xyz1", Child= lstChild}
};
Context.AddRangeAsync(lstparents);
Context.SaveChangesAsync();
}
我也嘗試了以下選項,但遇到了另一個問題。
選項1:
Public void main(string[] args)
{
List<Child> lstChild = new List<Child> ()
{
new Child{ ChildName= "Child1Name"},
new Child{ ChildName= "Child2Name"},
new Child{ ChildName= "Child3Name"}
};
List<Parent> lstparents = new List<Parent>()
{
new Parent {Name = "xyz", Child= lstChild },
new Parent {Name = "xyz1", Child= lstChild}
};
foreach(var item in lstparents)
{
Context.Add(item);
foreach(var child in lstChild)
{
Context.Add(child);
}
}
Context.SaveChangesAsync();
}
選項 2:在下面的代碼行中,我收到錯誤“當 IDENTITY_INSERT 設置為 OFF 時,無法為表中的標識列插入顯式值”
public void main(string[] args) {
List<Child> lstChild = new List<Child> ()
{
new Child{ ChildName= "Child1Name"},
new Child{ ChildName= "Child2Name"},
new Child{ ChildName= "Child3Name"}
};
List<Parent> lstparents = new List<Parent>()
{
new Parent {Name = "xyz", Child= lstChild },
new Parent {Name = "xyz1", Child= lstChild}
};
foreach(var item in lstparents)
{
Context.Entry(item).State=EntityState.Added;
foreach(var child in lstChild)
{
Context.Entry(child).State=EntityState.Added;
}
}
Context.SaveChangesAsync();
}
由於您有一對多關系(一個父母可以有多個孩子),您不能將一個孩子集添加到多個父母。 如果需要,則需要配置多對多關系。 因此,如果您想添加 2 個父母,則必須創建 2 個孩子列表,而不是一個
List<Child> childList1 = new List<Child> ()
{
new Child{ ChildName= "Child1Name"},
new Child{ ChildName= "Child2Name"},
new Child{ ChildName= "Child3Name"}
};
List<Child> childList2 = new List<Child> ()
{
new Child{ ChildName= "Child4Name"},
new Child{ ChildName= "Child5Name"},
new Child{ ChildName= "Child6Name"}
};
List<Parent> lstparents = new List<Parent>()
{
new Parent {Name = "parent1", Child= childList1 },
new Parent {Name = "parent2", Child= childList2 }
};
並且由於您的 main 不是異步的,因此您只能使用同步操作
Context.AddRange(lstparents);
Context.SaveChanges();
恕我直言,讓 ParentId 可以為空也許是個好主意
public long? ParentId { get; set;}
如果您需要添加多對多,則需要添加一個 ParentChild 表。
如果你使用net5+,ef core可以為你添加它,你只需要改變navigaion屬性
Public class Parent
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id {get; set;}// this is identity column
public string Name { get; set;}
public virtual ICollection<Child> Childs { get; set;}
}
Public class Child
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set;} // identity column
public string ChildName { get; set;}
public virtual ICollecton<Parent> Parents { get; set;}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.