EF 5.0 New Object: Assigning a Foreign Key Property Does Not Define the Foreign Key ID, or Add to the Collection

advertisements

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?


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