我正在尝试添加一些我创建的实体。当我尝试将有问题的实体添加到集合中时(请参阅下面的代码),我收到以下错误:
“无法添加或附加对象,因为其EntityReference的EntityKey属性值与此对象的EntityKey不匹配。”
我不知道它指的是什么实体键。这是代码,可能还有一个更好的方法来解决这个问题:
public Internship CreateInternship(Internship internshipToCreate)
{
try
{
Contact contactToCreate = new Contact();
contactToCreate.Fax = internshipToCreate.contacts.Fax;
contactToCreate.Extension = internshipToCreate.contacts.Extension;
contactToCreate.FirstName = internshipToCreate.contacts.FirstName;
contactToCreate.MiddleName = internshipToCreate.contacts.MiddleName;
contactToCreate.LastName = internshipToCreate.contacts.LastName;
contactToCreate.PhoneNumber = internshipToCreate.contacts.PhoneNumber;
contactToCreate.StreetAddress = internshipToCreate.contacts.StreetAddress;
contactToCreate.PostalCode = internshipToCreate.contacts.PostalCode;
contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail;
contactToCreate.statesReference.EntityKey =
new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID);
contactToCreate.countriesReference.EntityKey =
new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID);
_internshipEntities.AddToContactSet(contactToCreate);
_internshipEntities.SaveChanges();
try
{
Availability availabilityToCreate = new Availability();
availabilityToCreate.StartDate = internshipToCreate.availability.StartDate;
availabilityToCreate.EndDate = internshipToCreate.availability.EndDate;
availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable;
_internshipEntities.AddToAvailabilitySet(availabilityToCreate);
_internshipEntities.SaveChanges();
try
{
internshipToCreate.contactsReference.EntityKey =
new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
internshipToCreate.availabilityReference.EntityKey =
new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);
internshipToCreate.classificationsReference.EntityKey =
new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
internshipToCreate.educationReference.EntityKey =
new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);
_internshipEntities.AddToInternshipSet(internshipToCreate); //exception here
_internshipEntities.SaveChanges();
return internshipToCreate;
}
catch(Exception e)
{
throw e;
}
}
catch(Exception e)
{
throw e;
}
}
catch(Exception e)
{
throw e;
}
}
当我追踪时,除了错误之外没有其他信息,所以我甚至不确定哪个Key是问题。
编辑:这是最终工作的版本:
using (TransactionScope scope = new TransactionScope())
{
try
{
Contact contactToCreate = new Contact();
Availability availabilityToCreate = new Availability();
Internship i = new Internship();
// Set the contact entity values;
contactToCreate.Fax = internshipToCreate.contacts.Fax;
//...
//ommited for brevity
//...
contactToCreate.ContactEmail = internshipToCreate.contacts.ContactEmail;
// Set the contact entity references to existing tables
contactToCreate.statesReference.EntityKey =
new EntityKey("InternshipEntities.StateSet", "ID", internshipToCreate.contacts.states.ID);
contactToCreate.countriesReference.EntityKey =
new EntityKey("InternshipEntities.CountrySet", "ID", internshipToCreate.contacts.countries.ID);
// Add contact
_internshipEntities.AddToContactSet(contactToCreate);
// Set the availability entity values;
availabilityToCreate.StartDate = internshipToCreate.availability.StartDate;
availabilityToCreate.EndDate = internshipToCreate.availability.EndDate;
availabilityToCreate.Negotiable = internshipToCreate.availability.Negotiable;
// Add availability
_internshipEntities.AddToAvailabilitySet(availabilityToCreate);
//Add contact and availability entities to new internship entity
i.contacts = contactToCreate;
i.availability = availabilityToCreate;
// Set internship entity values;
i.UserID = internshipToCreate.UserID;
//...
//ommited for brevity
//...
i.Created = DateTime.Now;
// Set the internship entity references to existing tables
i.classificationsReference.EntityKey =
new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
i.educationReference.EntityKey =
new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);
// Add internship and save
_internshipEntities.AddToInternshipSet(i);
_internshipEntities.SaveChanges();
//commit transaction
scope.Complete();
return internshipToCreate;
}
catch (Exception e)
{
throw e;
}
}
答案 0 :(得分:3)
喂,
虽然我不确定问题是什么,但我有一个建议。您传入方法(internshipToCreate)的实习对象用于将值传输到您在方法 中实例化的其他实体(联系人,可用性) ,以及它们的创建工作得很好。
也许你应该尝试对实习做同样的事情?创建新的实习对象并通过从internshipToCreate对象获取所有值来设置它们,并将新创建的对象传递给_internshipEntities.AddToInternshipSet方法。
您可能在internshipToCreate对象上设置了一些您出于其他目的所需的值,其中一些实际上是导致异常。
并且,我不知道你的业务逻辑是什么,但是如果你把所有事情放在一个交易之下会更好,因为这样可能会发生前两个实体的创建,而第三个不是。
答案 1 :(得分:2)
这段代码对我来说并没有多大意义。在两种情况下,当您可以分配对象引用时,您将通过EntityKey。即,改变这个:
internshipToCreate.contactsReference.EntityKey =
new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
internshipToCreate.availabilityReference.EntityKey =
new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);
...为:
internshipToCreate.contacts = contactToCreate;
internshipToCreate.availability = availabilityToCreate;
在另外两种情况下,您似乎正在尝试分配已存在的对象的ID。这两条线,即使成功,在我看来,也无能为力:
internshipToCreate.classificationsReference.EntityKey =
new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
internshipToCreate.educationReference.EntityKey =
new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);
所以你可以摆脱它们。
进行这两项更改后会发生什么?