Question

I am attempting to add some entities that I have created. When I try and add the entity in question to the set (see code below) I get the following error:

"The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object."

I can't tell what entitykey it's referring to though. Here is the code, there is probably a much better way to pull this off as well:

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

}

There is no other information given besides the error when I trace through so I'm not even sure which Key is the issue.

EDIT: Here is the version that ended up working:

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;
    }
}
Was it helpful?

Solution

This code isn't making a lot of sense to me. In two cases, you're going through an EntityKey when you could just assign an object reference. I.e, change this:

internshipToCreate.contactsReference.EntityKey =
    new EntityKey("InternshipEntities.ContactSet", "ID", contactToCreate.ID);
internshipToCreate.availabilityReference.EntityKey =
    new EntityKey("InternshipEntities.AvailabilitySet", "ID", availabilityToCreate.ID);

...to:

internshipToCreate.contacts = contactToCreate;
internshipToCreate.availability = availabilityToCreate;

In the other two cases you seem to be attempting to assign the ID of the object which is already there. These two lines, even if successful, it seems to me, would do nothing:

internshipToCreate.classificationsReference.EntityKey =
    new EntityKey("InternshipEntities.ClassificationSet", "ID", internshipToCreate.classifications.ID);
internshipToCreate.educationReference.EntityKey =
    new EntityKey("InternshipEntities.EducationSet", "ID", internshipToCreate.education.ID);

So you can just get rid of them.

What happens when you make these two changes?

OTHER TIPS

Hallo,

although I'm not sure what the problem is I have a suggestion. The Internship object that you are passing into method (internshipToCreate) is used to transfer values to other entities (Contact, Availability) that you instantiated inside of the method, and their creation works just fine.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top