EF6 和 EF4.1 在文件层次结构中的区别

The difference between EF6 and EF4.1 in files hierarchy

我是 Entity Framework 的初学者。

我注意到当我使用 EF6 with Visual studio 2013:

我有 .Designer.cs 个带有此评论的空文件:

  // T4 code generation is enabled for model 'C:\Users\Luka\Desktop\Test\EF-db2008\AdventureWorks\AdventureWorksLib\AdventureWorksLib\AWLTModel.edmx'. 
    // To enable legacy code generation, change the value of the 'Code Generation Strategy' designer
    // property to 'Legacy ObjectContext'. This property is available in the Properties Window when the model
    // is open in the designer.

    // If no context and entity classes have been generated, it may be because you created an empty model but
    // have not yet chosen which version of Entity Framework to use. To generate a context class and entity
    // classes for your model, open the model in the designer, right-click on the designer surface, and
    // select 'Update Model from Database...', 'Generate Database from Model...', or 'Add Code Generation
    // Item...'.

.Context.tt 及其 .Context.cs 使用这样的代码:

 public partial class AWLTEntities : DbContext
    {
        public AWLTEntities()
            : base("name=AWLTEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Address> Addresses { get; set; }
        public virtual DbSet<Customer> Customers { get; set; }
    }

然后 .tt 文件和每个实体的 .cs 文件,例如 Customer.cs

使用这样的代码:

 public partial class Customer
    {
        public Customer()
        {
            this.NameStyle = false;
            this.CustomerAddresses = new HashSet<CustomerAddress>();
            this.Orders = new HashSet<Order>();
        }

        public int CustomerID { get; set; }
    }

当我使用 EF4.1 with visual studio 2010 时,这完全不同, 该型号的文件 .Designer.cs 后只有一个代码 !!


*.tt 个文件是 T4 templates used to autogenerate c# code from .EDMX file。这基本上是您的存储方案、概念方案以及它们之间的映射。 .context.cs 是您的 DbContext(以前的 ObjectContext),其他是实体。用于生成代码的工具是 EntityModelCodeGenerator,如 .edmx 文件的属性所示。所以它非常简单。

为了获得 OnPropertyChanged 用于 EntityObject class 您可以根据实体中的 this manual. But as you see it is not advised now... Probably all it'd be required to implement INotifyPropertyChanged 接口尝试自我跟踪实体(它们是 partial). EntityObject 已被代理 classes 取代。 EF 为每个实体生成它们(可以关闭)。并在那里进行跟踪。希望对您有所帮助!

更新。因此,您可以使用它在实体本身中生成验证,但这将是一些逻辑或事件处理。通常将其放入单独的模块中,放入 "free up" 的服务中。当然要看你的具体情况,要求。

所以让我们一步步说清楚:

  • 您已获得 .edmx 文件,该文件是从设计器创建的或从现有数据库生成的。但它只是一个 xml 文件,其中包含有关所使用的数据库结构的信息 - storage scheme, info about entities - conceptual schema 以及这两者之间的映射。它不包含任何可执行代码。需要生成此代码。

  • 要生成代码,将解析 .edmx 文件并创建包含实际可执行代码的 .cs 文件。可能使用 2 种方法:

    1. 生成器内置 Visual Studio - EntityModelCodeGenerator 工具。这是一种遗留方法,以前使用过(在您的案例中是在 Visaul Studio 2010 中)。这将只生成包含所有 类 的 .Designer.cs 文件。但是这种方法不是最好的——您不能根据需要修改生成过程(例如,将 DataMember 属性添加到生成的 类 或其他一些更改)。这就是为什么最好使用

    2. T4 templates. These are files with .tt extension. All they do is just execute their logic when Run custom tool is chosen in context menu, or .edmx file is changed. There is a set of available templates for generating EF code from .edmx, some info here. And because these are just regular files, you could modify them as you need (to get better editor experience use tangible T4 extension). Some basic info about the usage of T4 templates in EF here.

您可以独立于 Visual Studio 版本在这两种方法之间进行选择 - 只需更改 .edmx 文件属性中的 Code generation strategy 属性:

如果选择 Legacy ObjectContext 选项 - 您将获得单个 .Designer.cs 文件的第一种方式。如果 T4 - 那么 .Designer.cs 将为空(注释说使用了 T4 模板)并且 .tt 文件将生成使用的代码。因此,如果您需要与 VS 2010 中相同的代码 - 只需使用 Legacy ObjectContext 选项。

这两者之间的另一个区别是 1-st 生成遗留 ObjectContext 和派生自 EntityObect. They all will be in that .Designer.cs file. But that is not recommended any more (however you can still get corresponding T4 template - in that way you'll get your OnPropertyChanged and OnPropertyChanging back, because they are the methods of EntityObect class, however they are protected, so you might need to write some wrappers). But its better to use POCO classes and DbContext template 的实体 - VS 2013 在您的案例中使用的实体。然后,您将获得单独的 .Context.tt 生成 .Context.cs,其中包含派生的 DbContext,其中 DbSets 表示表,以及 .tt 文件生成实体 类.而 .tt.cs 之间的层次结构仅显示哪个 .cs 是由哪个 .tt 生成的,而只有 .cs 会在您的应用程序运行时实际编译和执行.

  • 现在关于 OnPropertyChanged - 那应该只是 INotifyPropertyChanged 接口的一个实现。但是看起来您正在使用生成 POCO 类 的模板。这是默认和推荐的选项,但要实现 INotifyPropertyChanged,您可能需要编辑模板或从 Visual Studio 在线图库中选择另一个模板。然而,这可能不是最好的架构解决方案,因为有时最好将实体和用于 UI/Business 逻辑的 类 分开。