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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:42:25+00:00 2026-05-18T00:42:25+00:00

My question can better be explained via example code. I am using POCO with

  • 0

My question can better be explained via example code. I am using POCO with change tracking proxies that is generated by the C# POCO generator by default. Please see below.

Assume you have Movie, MusicDirector and Director in the database and the relationship between them is a Director & MusicDirector can direct multiple movies and a movie can have only one Director and MusicDirector. Since I am a new user and cannot post images, here is my db structure.

Movie table has MovieId, Name,MusicDirectorId,DirectorId

Director table has DirectorId, Name

MusicDirector table has MusicDirectorId, Name

Here is the link to the diagram. https://i.stack.imgur.com/ce49r.png.

I am trying to insert a new movie and the director and musicdirector “already exists” in the db. Below is my code.

Movie movie = new Movie();            
        movie.Name = "Movie1";
        movie.Director = new Director() { Name = "DirectorA" };           
        movie.MusicDirector = new MusicDirector() { Name = "MusicDirectorA" };

        using (TestEFEntities ctx = new TestEFEntities())
        {
            movie.Director = ctx.Directors.Where(x => x.Name == movie.Director.Name).FirstOrDefault();
            movie.MusicDirector = ctx.MusicDirectors.Where(x => x.Name == movie.MusicDirector.Name).FirstOrDefault();                                    
            ctx.Movies.AddObject(movie);                    
            ctx.SaveChanges();
        }

Now when I do this, MusicDirector record is added again even though it is overwritten by the record from the db. You may think why I am keeping this line movie.Director = new Director() { Name = “DirectorA” }; initially, it is an Asp.net MVC app where Movie object is bound with the director and musicdirector names the user adds. So, the first 4 lines are done implicitly by MVC and think all the other lines are in the service layer. Am I missing something as this is a very basic scenario and the framework should handle it? Of course, one solution to correct this problem is to create a new Movie object and assign the records from db which I don’t want to do as I have to copy all the properties from the movie object sent by controller. How can I solve this problem?

Also this works correctly in Self-Tracking Entities. Is this some kind of a limitation to POCO and it would be great if someone can explain the behavior?

  • 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-18T00:42:26+00:00Added an answer on May 18, 2026 at 12:42 am

    If you are using POCOs, then your code should work as the way you expected. Alternatively, you can try to populated the FKs instead of assigning the whole object like this:

    using (TestEFEntities ctx = new TestEFEntities())
    {
        movie.DirectorID = ctx.Directors
            .Where(x => x.Name == movie.Director.Name).First().DirectorID;
    
        movie.MusicDirectorID = ctx.MusicDirectors
            .Where(x => x.Name == movie.MusicDirector.Name).First().MusicDirectorID;
    
        ctx.Movies.AddObject(movie);                    
        ctx.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a design question better explained with a Stack Overflow analogy: Users can
A few days ago I posted this question: switch statement and loops using jquery/javascript
In fact, this is the same question as this post: How can I make
I have a several questions about hibernate. In many questions here in stackoverflow, several
I'm not sure how to best explain this, so this may be a bit
To keep my integration tests independent I remove all old data and insert new
I haven't a clue about encryption at all. But I need it. How? Say
I suppose similar questions were already asked, but I was unable to find any.
I call Bind() and then Listen() on a System.Net.Sockets.Socket. None of these calls throw
I have a class where I use this to initialize a void* pointer. But

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.