如何为复合对象插入数据?

时间:2011-08-05 14:46:12

标签: entity-framework-4 wcf-ria-services

假设我有桌子,学生:

Person(id,....):id是主键和标识列。

学生(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”中。 声明已经终止。

如何解决这个问题?

0 个答案:

没有答案