1

我正在尝试添加我创建的一些实体。当我尝试将相关实体添加到集合中时(请参见下面的代码),我收到以下错误:

“无法添加或附加该对象,因为其 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;
    }

}

当我追踪时,除了错误之外没有提供其他信息,所以我什至不确定哪个键是问题所在。

编辑:这是最终工作的版本:

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;
    }
}
4

2 回答 2

3

你好,

虽然我不确定问题是什么,但我有一个建议。您传递给方法 (internshipToCreate) 的实习对象用于将值传输到您在方法内部实例化的其他实体(联系人、可用性) ,并且它们的创建工作正常。

Maybe you should try to do the same with Internship? Create new Internship object and set all values you have by taking them from internshipToCreate object, and than that newly created object pass to the _internshipEntities.AddToInternshipSet method.

It is possible that you've set some values on internshipToCreate object that you needed for other purposes, and that some of those is actually causing the exception.

And, I don't know what you business logic is, but it would be better if you put all under one transaction, because like this it may happen that first two entities are created, and third one not.

于 2009-09-03T08:44:24.457 回答
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);

所以你可以摆脱它们。

当您进行这两项更改时会发生什么?

于 2009-09-02T19:51:58.490 回答