EntityFramework CTP5更改跟踪 [英] EntityFramework CTP5 change tracking

查看:108
本文介绍了EntityFramework CTP5更改跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CTP5 DBContext重现与EntityObject相同的行为来进行更改跟踪。考虑电影和导演的表。每个导演的关系只有1位电影导演和多部电影。

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();

如果我使用EntityObject运行这个代码,这个代码将创建一个新的导演dir1,因为更改是跟踪。如果我使用CTP 5 DbContext生成器运行此代码,则不会创建新的Directordir1。我在Movie和Director对象中将属性更改为 virtual 。以下是代码。

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; }    
}

我有3个问题。


  • 我在这里遗漏了什么吗?即使我为每个财产保持虚拟,但该对象未被跟踪。为什么?

  • 我必须像在EF4 POCO中一样写协会修复逻辑吗?

  • 如果是这样,为什么在DbContext T4生成器中删除了Association Fixup代码?

推荐答案

当然,新导演没有得到保存,因为您将新电影导演改为在你的代码稍后的一个现有的一个,尝试这个,你会得到他们都保存到DB:

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();

您可以编写自己的Association fixup逻辑,但这将保留保留关联的端点同步,并且与您在这里显示的代码无关。

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.

当您使用EntityObjects时,代码将新的导向器保存到DB中的原因是因为一个概念称为关系跨度。关系跨度定义当
ObjectContext在您将其连接到另一个附加实体时将自动附加实体。如果分离的对象是新的,当它附加到上下文时,其EntityState将被添加。然而,即使您使用POCO代理(即使您的导航属性虚拟化),此关系跨度行为也不会实现。

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).

这篇关于EntityFramework CTP5更改跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆