Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 356691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:08:08+00:00 2026-05-12T12:08:08+00:00

I am attempting to add some entities that I have created. When I try

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:08:08+00:00Added an answer on May 12, 2026 at 12:08 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to add some multi-threading to a WPF application I have created
I have am attempting to add some input fields to a jquery ui dialog
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
I am attempting to add validation to my application. I have some rules I
I have a class that inherits from a dictionary in order to add some
I am relatively new to unit testing and was attempting to add some code
I am attempting add some tests to an existing QT GUI application using QTest.
I'm having some trouble with a word add-in. I'm attempting to create a new
Im attempting to make a little app that lets you add text boxes to
I'm attempting to add a property in a partial class to an entity framework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.