代码如下,采用CODE FIRST EF6.0:
public class Book { public int Id{get;set;} public string Name{get;set;} public int AuthorId {get;set;} [ForeignKey("AuthorId ")] public virtual Person Author {get;set;} } public class Person { public int Id{get;set;} public string Name{get;set;} } 更新语句: var p=context.Persons.Single(t=>t.Id==1); var book=context.Books.Singlie(t=.t.Id==1); book.Author =null; book.AuthorId =1; context.SaveChanges(); //报错如后面描述 换成如下仍是同样报错: book.Author =p; context.SaveChanges(); //报错如后面描述
报错信息:
A referential integrity constraint violation occurred: The property value(s) of 'Person.ID' on one end of a relationship do not match the property value(s) of 'Book.AuthorId' on the other end.
目前网上有人遇到过,但没有好的解决方案,求高人指点!
比如:
http://stackoverflow.com/questions/16128664/a-referential-integrity-constraint-violation-occurred
为啥就是没有有回答啊!
试试把主键都加上[Key]
都有加的!