如何为复合对象插入数据?
假设我有表 Person,student:
Person(id, ....): id 是主键和标识列。
Student(id, .....): id是主键,外键连接到person->ID
在ria服务元数据中,将Student设置为Person的组合:
[Include]
[Composition]
public Student Student { get; set; }
然后我想插入一个新的学生数据。我所做的是这样的:
Person p = new person(){....};
p.Student = new Student{....};
_context.People.AddObject(p);
_context.SaveChanges();
然后我收到错误:
System.Data.UpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_Student_Person”冲突。冲突发生在数据库“MyDB”、表“dbo.Person”、列“id”中。 该声明已终止。
如何解决这个问题?
Suppose I have table Person, student:
Person(id, ....): id is primary key and identity column.
Student(id, .....): id is primary key and foreign key connect to person->ID
In ria service meta data, set Student as composite of Person:
[Include]
[Composition]
public Student Student { get; set; }
then I want to insert a new student data. what I did is something like:
Person p = new person(){....};
p.Student = new Student{....};
_context.People.AddObject(p);
_context.SaveChanges();
then I got error:
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Student_Person". The conflict occurred in database "MyDB", table "dbo.Person", column 'id'.
The statement has been terminated.
How to fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论