[英]Entity Framework: Improving Performance when inserting a record in a table with many records
[英]Entity Framework: records in child table are not inserting
父表中的記錄保存成功,但子表記錄未保存且不會出現錯誤,使用具有可用存儲庫模式的實體框架here 。
下面是我的表的示例架構設計,可能是一個特例。
考慮上面的表格,如果我嘗試將 Table2 和 Table3 中的記錄一起插入,則沒有記錄插入到 Table3 中。
如果需要進一步澄清這個問題,請告訴我。
提前致謝!!
編輯 1
下面是我用來插入父和子記錄的代碼。
var parent = new Table2()
{
Table1Id = 1,
SecondColumn = 1
};
parent.Tables3.Add(new Table3()
{
Table1Id = 1,
SecondColumn = 1,
AnotherColumn = 1
});
context.Set<Table2>().Attach(parent);
context.Entry<Table2>(parent).State = EntityState.Added;
context.SaveChanges();
編輯 2
public partial class Table2
{
public Table2()
{
this.Tables3 = new HashSet<Table3>();
}
public int Table1Id { get; set; }
public int SecondColumn { get; set; }
public virtual ICollection<Table3> Tables3 { get; set; }
}
public partial class Table3
{
public int Table1Id { get; set; }
public int SecondColumn { get; set; }
public int AnotherColumn { get; set; }
}
您應該執行以下兩項操作之一:
使用添加而不是附加
var parent = new Table2() { Table1Id = 1, SecondColumn = 1 }; parent.Tables3.Add(new Table3() { Table1Id = 1, SecondColumn = 1, AnotherColumn = 1 }); context.Set<Table2>().Add(parent); context.SaveChanges();
在這種情況下,還將添加所有導航屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.