我使用ASP.NET 5實體框架7. 我有這個型號:子元素在實體框架插入7
public class ParentModel
{
public Guid Id { get; set; }
public virtual ChildModel Children { get; set; }
}
public class ChildModel
{
public Guid Id { get; set; }
public virtual AnotherChildModel AnotherChild { get; set; }
}
public class AnotherChildModel
{
public Guid Id { get; set; }
public string Text { get; set; }
}
當我試圖ParentModel添加到數據庫中,這不是」自動不要再增加ChildModel和AnotherChildModel數據庫,而ParentModel代碼完全正確,例如:
var parent = new ParentModel() { Children = new ChildModel() { AnotherChild = new AnotherChildModel() { Text = "sometext" }}};
所以,簡單parentSet.Add(parent)
不起作用,有另一種方式,除了手動添加所有型號的套?
編輯:
例外,我有:
DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ParentModel_ChildModel_ChildrenId". The conflict occurred in database "aspnet5-WebApplication1-922849d0-b7da-4169-8150-9a2d05240a47", table "dbo.ChildModel", column 'Id'. The statement has been terminated.
不,你需要添加子和父類單獨 –
您正在使用的初始化'ChildModel'似乎並不模型... – David
@大衛編輯以 –