在文件层次EF6和EF4.1之间的差 [英] The difference between EF6 and EF4.1 in files hierarchy

查看:550
本文介绍了在文件层次EF6和EF4.1之间的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者到实体框架。



我注意到,当我使用与Visual Studio 2013 EF6



了.Designer.cs 本评论空文件:

  // T4代码生成的模型'C启用:\Users\Luka\Desktop\Test\EF-db2008\AdventureWorks\ AdventureWorksLib\AdventureWorksLib\AWLTModel.edmx。 
//要使用传统代码的产生,代码生成策略设计师
的值//属性更改为ObjectContext的遗产。此属性是在属性窗口时可用的模型
//是在设计开放。

//如果没有上下文和实体类已经产生,这可能是因为你创建了一个空模型,但//尚未选择要使用的实体框架版本
。要生成上下文类和实体
//类模型中,打开设计模型,设计师表面上单击鼠标右键,并
//选择从数据库更新模型...... 从生成模型库...或添加代码生成
//项...。



.Context.tt .Context.cs
使用如下代码:

 公共部分类AWLTEntities:的DbContext 
{
公共AWLTEntities()
:基地(NAME = AWLTEntities)
{
}

保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
抛出新UnintentionalCodeFirstException();
}

公共虚拟DbSet<地址>地址{搞定;组; }
公共虚拟DbSet<客户>客户{搞定;组; }
}



然后 .TT 与像 Customer.cs


$ b每一个实体的.cs 文件档案$ b

通过这样的代码:

 公共部分类客户
{
公众客户( )
{
this.NameStyle = FALSE;
this.CustomerAddresses =新的HashSet< CustomerAddress>();
this.Orders =新的HashSet<排序>();
}

公众诠释客户ID {搞定;组; }
}






这的完全不同当我使用 EF4.1与Visual Studio 2010
,只有一个隐藏文件中的代码了.Designer.cs 为模型!







  • 能有人帮助我了解什么是所有这些文件为
    .Context.tt .Context.cs 。 TT 的.cs ?,什么是不同的
    在文件中这两种情况之间的层次结构(EF6, EF4.1)

  • 我找不到 OnPropertyChanging(价值)及?; OnPropertyChanged()
    EF6 来验证我的实体!!为什么这些方法不再存在,如何来验证我的
    ?如果他们不存在性能


解决方案

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




  • 您已经得到了你的的.edmx 文件,该文件是从创建设计师或从现有数据库生成的。但它只是一个XML文件,其中包含有关所使用的数据库结构的信息 - 存储方案,关于实体的信息 - 概念模式以及之间的映射这两个。它不包含任何可执行代码。需要生成此代码。


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




    1. 发电机集结在Visual Studio - 的的 EntityModelCodeGenerator 工具。这是一个传统的做法,这是(在你的案件在2010年VISAUL工作室)以前使用的。这将生成只有了.Designer.cs 与它里面的所有文件类。但这种方法是不是最好的 - 你不能修改生成过程为您的需求(例如,添加数据成员属性生成的类或一些其他的变化)。这就是为什么它是更好地使用


    2. T4模板。这些是 .TT 扩展名的文件。他们所要做的仅仅是执行他们的逻辑,当运行定制工具选择在上下文菜单或的.edmx 文件被修改。有从的.edmx 生成代码EF,一些信息的此处。而且,由于这些都只是普通文件,你可以根据需要修改它们(以获得更好的体验编辑使用的有形T4扩展)。关于T4模板EF 这里使用的一些基本信息。





您可以独立的Visual Studio版本这两个方法之间做出选择 - 只是改变了代码生成策略的.edmx 文件的属性,属性:





如果在传统的ObjectContext 选项被选择 - 你得到一个了.Designer.cs 文件1-ST的方式。如果 T4 - 那么了.Designer.cs 将是空的(有评论说,T4模板使用)和 .TT 文件将生成使用的代码。所以,如果你需要相同的代码在VS 2010中 - 只需使用传统的ObjectContext 选项



那些之间的另一个区别二是,1 - ST产生遗留的ObjectContext 和从的 EntityObect 。他们都将在了.Designer.cs 文件。但是,不建议任何更多的(但是你仍然可以得到相应的T4模板 - 那样你会得到你的 OnPropertyChanged OnPropertyChanging 回来了,因为他们的方法 EntityObect 类,但他们受到保护,所以你可能需要写一些包装)。但是它能够更好地使用 POCO类和模板的DbContext - 一该VS 2013中的情况下使用。然后你会得到不同的 .Context.tt 生成 .Context.cs 的衍生的DbContext 在其与 DbSets 代表表和 .TT 文件生成实体类。之间的层次 .TT 的.cs 只显示了哪些的.cs 是由 .TT ,而只有的.cs 将被实际遵守和执行时你的应用程序运行产生




  • 现在关于 OnPropertyChanged - 这应该只是<$的实现C $ C> INotifyPropertyChanged的接口。不过看起来你正在使用生成的POCO类模板。这是建议的默认选项,但要获得 INotifyPropertyChanged的的实施可能需要编辑模板或选择从Visual Studio在线艺廊另一个。那是,不过,可能不是最好的建筑解决方案,因为它有时是更好的独立个体,而你使用的UI /业务逻辑的类。


I'm a beginner to Entity Framework .

I notice that When I use EF6 with Visual studio 2013:

I have .Designer.cs empty file with this comment:

  // 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 and its .Context.cs with code like this:

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

And then .tt file with .cs file for each entity like Customer.cs

With code like this :

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

        public int CustomerID { get; set; }
    }


This 's totally different when i use EF4.1 with visual studio 2010 , there's only one code behind file .Designer.cs for the model !!


  • Could some one help me to understand what are all these files for .Context.tt , .Context.cs,.tt,.cs ?and what 's different in files hierarchy between the two cases(EF6,EF4.1)?
  • I can't find OnPropertyChanging(Value) & OnPropertyChanged() in EF6 to validate my entities !!Why these methods no longer exist and how to validate my properties if they do not exist?

解决方案

So lets make it clear step by step:

  • You've got your .edmx file, which was created from designer or generated from existing database. But it's only an xml file, which contains info about the database structure that is used - storage scheme, info about entities - conceptual schema and mappings between those two. It doesn't contain any executable code. This code needs to be generated.

  • To generate the code the .edmx file will be parsed and .cs files will be created that contain actual executable code. 2 approaches might be used:

    1. Generator build-in the Visual Studio - the EntityModelCodeGenerator tool. That is a legacy approach, that was used previously (in Visaul Studio 2010 in your case). This will generate only the .Designer.cs file with all classes inside it. But this approach is not the best - you cannot modify generation process for your needs (say, add DataMember attribute to generated classes or some other changes). That's why it's better to use

    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.

You can choose between these 2 approaches independently of Visual Studio version - just change the Code generation strategy property in the properties of your .edmx file:

If the Legacy ObjectContext option is chosen - you get 1-st way with single .Designer.cs file. If T4 - then the .Designer.cs will be empty (with comments saying that T4 templates are used) and .tt files will generate the used code. So if you need the same code as in VS 2010 - just use Legacy ObjectContext option.

Another difference between those two is that 1-st generates legacy ObjectContext and entities that are derived from 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 - the one that VS 2013 used in your case. Then you'll get separate .Context.tt to generate .Context.cs with derived DbContext in it with DbSets representing tables, and .tt file to generate the entity classes. And the hierarchy between .tt and .cs only shows which .cs were generated by which .tt, while only .cs will be actually complied and executed when your app runs.

  • And now regarding OnPropertyChanged - that should be just an implementation of INotifyPropertyChanged interface. However looks like you are using template that generate POCO classes. That is the default and recommended option, but to get the implementation of INotifyPropertyChanged you might need to edit the template or choose another one from Visual Studio Online gallery. That's, however, might not be the best architectural solution, because it is sometimes better to separate entities and the classes you use for UI/Business logic.

这篇关于在文件层次EF6和EF4.1之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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