我正在按照本教程进行一对一的联系,但无济于事。http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx
这是我的快速模拟
public class Information
{
public int InformationId { get; set; }
public string Thing1 {get; set;}
public string Thing2 { get; set;}
public ICollection<Item> Items { get; set; }
public Account Account { get; set; }
}
public class Account
{
[Key, ForeignKey("Information")]
public int InformationId { get; set; }
public string Thing1 { get; set; }
public Information Information { get; set; }
}
我对它执行
var information = new List<Information>{
new Information(){
Thing1 = "this",Thing2="that"
}
};
information.ForEach(s => context.Information.Add(s));
context.SaveChanges();
var account = new List<Account>{
new Account(){Thing1="one"}
};
account.ForEach(s => context.Accounts.Add(s));
context.SaveChanges();
这是错误的
{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Account_dbo.Information_InformationId\". The conflict occurred in database \"TestEF\", table \"dbo.Information\", column 'InformationId'.\r\nThe statement has been terminated."}
先保存帐户,然后再保存信息
var account = new List<Account>{
new Account(){Thing1="one"}
};
account.ForEach(s => context.Accounts.Add(s));
context.SaveChanges();
var information = new List<Information>{
new Information(){
Thing1 = "this",Thing2="that"
}
};
information.ForEach(s => context.Information.Add(s));
context.SaveChanges();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句