EntityFramework CTP5 change tracking

2019-08-07 18:54发布

I am trying to reproduce the same behavior as EntityObject using CTP5 DBContext for change tracking. Consider the tables Movie and Director. Relationship is only 1 director for a movie and multiple movies for each director.

var movie = new Movie();
            movie.Name = "ABCD";
            ctx.Movies.Add(movie);//ctx.Movies.AddObject(movie); 
            movie.Director = new Director() { Name = "dir1" };
            var existingDirector = ctx.Directors.Where(a => a.Name == "dir2").FirstOrDefault();
            movie.Director = existingDirector;
            ctx.SaveChanges();

If I run this using EntityObject, this code would create a new director "dir1" as the changes are tracked. If I run this code using CTP 5 DbContext generator, the new director "dir1" is not created. I changed the properties to be virtual in both Movie and Director objects. Below is the code.

public partial class Director
{
    public Director()
    {
        //this.Movies = new HashSet<Movie>();
    }

    // Primitive properties

    public virtual int DirectorId { get; set; }
    public virtual string Name { get; set; }

    // Navigation properties

    public virtual ICollection<Movie> Movies { get; set; }

}
public partial class Movie
{
    public Movie()
    {
        //this.Actors = new HashSet<Actor>();
    }

    // Primitive properties

    public virtual int MovieId { get; set; }
    public virtual Nullable<int> DirectorId { get; set; }
    public virtual string Name { get; set; }

    // Navigation properties

    public virtual Director Director { get; set; }    
}

I have 3 questions.

  • Am I missing anything here? Even though I kept "virtual" for every property, the object is not being tracked. Why?
  • Do I have to write "Association fixup" logic as was done in EF4 POCOs?
  • If so, why was the Association fixup code removed in DbContext T4 generator?

2条回答
forever°为你锁心
2楼-- · 2019-08-07 19:24

I think the reason that this isn't working the way you expect it to is that you are creating an instance of the Movie class itself (I.e. using the new operator), rather than the dynamic proxy. The Movie class itself doesn't have built-in change tracking. Hence, when you set the Director property, no notification is sent to the DbContext. Even though you added the Movie to the DbContext, you are still referencing the original object, not a proxy. I think if you created the object using DbSet.Create() (http://msdn.microsoft.com/en-us/library/gg696685(v=vs.103).aspx) it would work.

查看更多
可以哭但决不认输i
3楼-- · 2019-08-07 19:33

Of course the new director does not get saved because you changed the new movie's director to an existing one at some later point in your code, try this one and you'll get them both saved into DB:

var movie = new Movie();
movie.Name = "ABCD";
ctx.Movies.Add(movie);
movie.Director = new Director() { Name = "dir1" };    
//movie.Director = existingDirector;
ctx.SaveChanges();

You can write your own Association fixup logic but that's going to take care of keeping the endpoints of your associations in sync, and has nothing to do with the code you showed here.

The reason that your code saves the new director into the DB when using EntityObjects is because of a concept that called Relationship Span. The relationship span defines that the ObjectContext will automatically attach an entity when you have joined it to another attached entity. If that detached object is new, when it is attached to the context its EntityState will be Added. However this Relationship Span behavior is not implemented even when you are using POCO proxies (i.e. making your navigation properties virtual).

查看更多
登录 后发表回答