【发布时间】:2012-09-01 01:51:03
【问题描述】:
我有 2 个实体对象“Persons”和“Seminar”。关系 - 多对多。一个项目包括 - EF4.0/STE、WCF 和 WinForms。
当我尝试将人员添加到研讨会时
public void AddPersonsToSeminar(Seminar seminar, List<Person> persons)
{
using (T3EntitiesConn context = new T3EntitiesConn())
{
if (seminar != null)
{
context.Seminar.Attach(seminar);
foreach (Person person in persons)
{
if (!seminar.Person.Any(p => p.ID == person.ID))
{
seminar.Person.Add(person);
context.Seminar.ApplyChanges(seminar);
}
}
context.SaveChanges();
我有例外 -
The property 'ID' is part of the object's key and cannot be changed. Changes to key properties can only be made when the object is not being tracked or is in the Added state.
请解释一下如何解决它 谢谢
【问题讨论】:
-
题外话,“persons”应改为“people”。
标签: c# wcf entity-framework entity-framework-4 self-tracking-entities