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 7725693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:00:14+00:00 2026-06-01T05:00:14+00:00

In Entity Framework 4.2 I have a Trips entity that can have 0..* PlacesOfInterest

  • 0

In Entity Framework 4.2 I have a Trips entity that can have 0..* PlacesOfInterest and 0..* Photos. Places of Interest has 1 Trip and 0..* Photos. Photos have 1 Trip and 0..1 Places of Interest.

When I try to add a Photo, I use this method:

    public static Guid Create(string tripId, Model.Photo instance)
    {
        var context = new Model.POCOTripContext();
        var cleanPhoto = new Model.Photo();
        cleanPhoto.Id = Guid.NewGuid();
        cleanPhoto.Name = instance.Name;
        cleanPhoto.URL = instance.URL;
        //Relate the POI
        cleanPhoto.PlaceOfInterest = Library.PlaceOfInterest.Get(instance.PlaceOfInterestId);
        context.PlacesOfInterest.Attach(cleanPhoto.PlaceOfInterest);
        //Relate the trip
        cleanPhoto.Trip = Library.Trip.Get(new Guid(tripId));
        context.Trips.Attach(cleanPhoto.Trip);
        //Add the photo
        context.Photos.AddObject(cleanPhoto);
        context.SaveChanges();
        return cleanPhoto.Id;
    }

When I test this, I get the following when the Trip is attached:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

The Trip does appear in the context object, but the PlacesOfInterest does too before the Attach statement. I don’t understand how this works, can someone clarify?

EDIT: Here are the POI and Trip Getters

    public static Model.Trip Get(Guid tripId)
    {
        using (Model.POCOTripContext context = new Model.POCOTripContext())
        {
            var tripEntity = context.Trips.Include("PlacesOfInterest").Include("PlacesOfInterest.PoiAttributes").Include("Photos").FirstOrDefault(c => c.Id == tripId) ?? new Model.Trip();
            return tripEntity;
        }
    }

    public static Model.PlaceOfInterest Get(Guid poiId)
    {
        using (Model.POCOTripContext context = new Model.POCOTripContext())
        {
            var poiEntity = context.PlacesOfInterest.Include("PoiAttributes").FirstOrDefault(c => c.Id == poiId) ?? new Model.PlaceOfInterest();
            return poiEntity;
        }
    }

Thanks

S

  • 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-06-01T05:00:16+00:00Added an answer on June 1, 2026 at 5:00 am

    This…

    context.Trips.Include("PlacesOfInterest")....
    

    …will load the PlacesOfInterest with the trip. When you attach the trip to the other context trip.PlacesOfInterest get attached as well. Because you already have attached a PlaceOfInterest before (which has an Id of a PlaceOfInterest in the collection) you are attaching two objects of the same type with the same key. This causes the exception.

    You can actually simplify your code: You don’t need to load the entities because you have their primary key. Then you can just create new instances with that key and attach it:

    cleanPhoto.PlaceOfInterest = new PlaceOfInterest
                                 { Id = instance.PlaceOfInterestId };
    context.PlacesOfInterest.Attach(cleanPhoto.PlaceOfInterest);
    
    cleanPhoto.Trip = new Trip { Id = new Guid(tripId) };
    context.Trips.Attach(cleanPhoto.Trip);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object defined by my entity framework model that has navigation properties,
I'm using the Play! framework and I have a model (an Entity) that has
We have an entity framework model with has some inheritance in it. The following
I have an Entity Framework generated table with values in a td that I
I have an Entity in Code First Entity framework that currently looks like this:
I have a web application that is using Entity Framework to query a SQL
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
I'm using Entity Framework and I have a custum IQueryProvider. I use the Execute
I have created a Dynamic Data site against an Entity Framework Model I have
I have an Entity Framework data model. Part of the model is a Customer

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.