我有2个实体对象“人”和“研讨会”。关系-多对多。一个项目包括-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.
请解释一下如何解决
谢谢
最佳答案
问题可能是您的研讨会和人员来自不同的对象上下文,而您正在创建新的上下文。您将研讨会正确地附加到该上下文,但是您是将人员从旧上下文添加到新创建的上下文中吗?