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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:30:17+00:00 2026-06-16T23:30:17+00:00

EF 5.0, using code-first on existing database workflow. Database has your basic SalesOrder and

  • 0

EF 5.0, using code-first on existing database workflow.
Database has your basic SalesOrder and SalesOrderLine tables with required foreign key on the SalesOrderLine as follows;

public class SalesOrder
{
    public SalesOrder()
    {
        this.SalesOrderLines = new List<SalesOrderLine>();
    }

    public int SalesOrderID { get; set; }
    public int CustomerID { get; set; }
    public virtual Customer Customer { get; set; }

    public virtual ICollection<SalesOrderLine> SalesOrderLines { get; set; }
}

public class SalesOrderLine
{
    public SalesOrderLine()
    {
    }
    public int SalesOrderLineID { get; set; }
    public int SalesOrderID { get; set; }

    public virtual SalesOrder SalesOrder { get; set; }
}
public SalesOrderLineMap()
{
    // Primary Key
    this.HasKey(t => t.SalesOrderLineID);
    // Table & Column Mappings
    this.ToTable("SalesOrderLine");
    this.Property(t => t.SalesOrderLineID).HasColumnName("SalesOrderLineID");
    this.Property(t => t.SalesOrderID).HasColumnName("SalesOrderID");

    // Relationships
    this.HasRequired(t => t.SalesOrder)
        .WithMany(t => t.SalesOrderLines)
        .HasForeignKey(d => d.SalesOrderID);
}

Now according to this page:
http://msdn.microsoft.com/en-us/data/jj713564

…we are told that:

The following code removes a relationship by setting the foreign key
to null. Note, that the foreign key property must be nullable.

course.DepartmentID = null;

Note: If the reference is in the added state (in this example, the
course object), the reference navigation property will not be
synchronized with the key values of a new object until SaveChanges is
called. Synchronization does not occur because the object context does
not contain permanent keys for added objects until they are saved. If
you must have new objects fully synchronized as soon as you set the
relationship, use one of the following methods.

By assigning a new object to a navigation property. The following code
creates a relationship
between a course and a department. If the
objects are attached to the context, the course is also added to the
department.Courses collection, and the corresponding foreign key
property on the course object is set to the key property value of the
department.

course.Department = department;

…sounds good to me!

Now my problem:
I have the following code, and yet both of the the Asserts fail – why?

    using (MyContext db = new MyContext ())
    {
        SalesOrder so = db.SalesOrders.First();
        SalesOrderLine sol = db.SalesOrderLines.Create();
        sol.SalesOrder = so;

        Trace.Assert(sol.SalesOrderID == so.SalesOrderID);
        Trace.Assert(so.SalesOrderLines.Contains(sol));
    }

Both objects are attached to the context – are they not? Do I need to do a SaveChanges() before this will work? If so, that seems a little goofy and it’s rather annoying that I need to set all of the references on the objects by hand when a new object is added to a foreign-key collection.

— UPDATE —

I should mark Gert’s answer as correct, but I’m not very happy about it, so I’ll wait a day or two. …and here’s why:

The following code does not work either:

SalesOrder so = db.SalesOrders.First();
SalesOrderLine sol = db.SalesOrderLines.Create();
db.SalesOrderLines.Add(sol);

sol.SalesOrder = so;
Trace.Assert(so.SalesOrderLines.Contains(sol));

The only code that does work is this:

SalesOrder so = db.SalesOrders.First();
SalesOrderLine sol = db.SalesOrderLines.Create();

sol.SalesOrder = so;
db.SalesOrderLines.Add(sol);

Trace.Assert(so.SalesOrderLines.Contains(sol));

…in other words, you have to set all of your foreign key relationships first, and then call TYPE.Add(newObjectOfTYPE)
before any of the relationships and foreign-key fields are wired up. This means that from the time the Create is done until the time you do the Add(), the object is basically in a half-baked state. I had (mistakenly) thought that since I used Create(), and since Create() returns a sub-classed dynamic object (as opposed to using “new” which returns a POCO object) that the relationships wire-ups would be handled for me. It’s also odd to me, that you can call Add() on an object created with the new operator and it will work, even though the object is not a sub-classed type…

In other words, this will work:

    SalesOrder so = db.SalesOrders.First();
    SalesOrderLine sol = new SalesOrderLine();

    sol.SalesOrder = so;
    db.SalesOrderLines.Add(sol);

    Trace.Assert(sol.SalesOrderID == so.SalesOrderID);
    Trace.Assert(so.SalesOrderLines.Contains(sol));

…I mean, that’s cool and all, but it makes me wonder; what’s the point of using “Create()” instead of new, if you’re always going to have to Add() the object in either case if you want it properly attached?

Most annoying to me is that the following fails;

SalesOrder so = db.SalesOrders.OrderBy(p => p.SalesOrderID).First();
SalesOrderLine sol = db.SalesOrderLines.Create();

sol.SalesOrder = so;
db.SalesOrderLines.Add(sol);

 // NOTE: at this point in time, the SalesOrderId field has indeed been set to the SalesOrderId of the SalesOrder, and the Asserts will pass...
Trace.Assert(sol.SalesOrderID == so.SalesOrderID);
Trace.Assert(so.SalesOrderLines.Contains(sol));

sol.SalesOrder = db.SalesOrders.OrderBy(p => p.SalesOrderID).Skip(5).First();

 // NOTE: at this point in time, the SalesOrderId field is ***STILL*** set to the SalesOrderId of the original SO, so the relationships are not being maintained! 
// The Exception will be thrown!
if (so.SalesOrderID == sol.SalesOrderID)
    throw new Exception("salesorderid not changed");

…that seems like total crap to me, and makes me feel like the EntityFramework, even in version 5, is like a minefield on a rice-paper bridge. Why would the above code not be able to sync the SalesOrderId on the second assignment of the SalesOrder property? What essential trick am I missing here?

  • 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-16T23:30:18+00:00Added an answer on June 16, 2026 at 11:30 pm

    I’ve found what I was looking for! (and learned quite a bit along the way)

    What I thought the EF was generating in it’s dynamic proxies were “Change-Tracking Proxies”. These proxy classes behave more like the old EntityObject derived partial classes from the ADO.Net Entity Data Model.

    By doing some reflection on the dynamically generated proxy classes (thanks to the information i found in this post: http://davedewinter.com/2010/04/08/viewing-generated-proxy-code-in-the-entity-framework/ ), I saw that the “get” of my relationship properties was being overridden to do Lazy Loading, but the “set” was not being overriden at all, so of course nothing was happening until DetectChanges was called, and DetectChanges was using the “compare to snapshot” method of detecting changes.

    Further digging ultimately lead me to this pair of very informative posts, and I recommend them for anyone using EF:
    http://blog.oneunicorn.com/2011/12/05/entity-types-supported-by-the-entity-framework/

    http://blog.oneunicorn.com/2011/12/05/should-you-use-entity-framework-change-tracking-proxies/

    Unfortunately, in order for EF to generate Change-Tracking Proxies, the following must occur (quoted from the above):

    • The rules that your classes must follow to enable change-tracking
      proxies are quite strict and restrictive. This limits how you can
      define your entities and prevents the use of things like private
      properties or even private setters. The rules are: The class must be
      public and not sealed. All properties must have public/protected
      virtual getters and setters.
      Collection navigation properties must be
      declared as ICollection<T>. They cannot be IList<T>, List<T>,
      HashSet<T>, and so on.

    • Because the rules are so restrictive it’s easy to get something wrong and the result is you won’t get a change-tracking proxy. For example,
      missing a virtual, or making a setter internal.

    …he goes on to mention other things about Change-Tracking proxies and why they may show better or worse performance.

    In my opinion, the change-tracking proxy classes would be nice as I’m coming from the ADO.Net Entity Model world, and I’m used to things working that way, but I’ve also got some rather rich classes and I’m not sure if I will be able to meet all of the criteria. Additionally that second bullet point makes me rather nervous (although I suppose I could just create a unit test that loops through all of my entities, does a Create(0 on each and then tests the resulting object for the IEntityWithChangeTracker interface).

    By setting all of my properties to virtual in my original example I did indeed get IEntityWithChangeTracker typed proxy classes, but I felt a little … I don’t know… “dirty” …for using them, so I think I will just have to suck it up and remember to always set both sides of my relationships when doing assignments.

    Anyway, thanks for the help!

    Cheers,
    Chris

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

Sidebar

Related Questions

I followed Using EF Code First with an Existing Database tutorial step by step
I'm using EF Code First with a pre-existing database. Two objects: public Foo {
I am building an EF Code First driven application using an existing MSSQL database.
I'm working with an existing database and using EF 4.3 Code First. I have
I'm using Entity Framework Reverse Engineer Code First on an existing database. The classes
I'm trying to map an existing database using Entity Framework Code First Fluent API.
I am using Entity Framework 4.3 code first using an already existing database. There
Easier to show by example -- I'm using code-first to construct a database. I
I successfully created database on my local server using Code First, migrated it to
I am using EF Code First to create a database on local .\SQLEXPRESS. Among

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.