The difference between EF6 and EF4.1 in files hierarchy

梦想与她 提交于 2019-12-22 04:39:20

问题


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?

回答1:


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.



回答2:


*.tt files are T4 templates used to autogenerate c# code from .EDMX file. This is basically your storage scheme, conceptual scheme and a mapping between them. The .context.cs is your DbContext (former ObjectContext) and other are entities. The tool used to generate code is EntityModelCodeGenerator as seen in properties of .edmx file. So it is pretty straight forward.

In order to get OnPropertyChanged that was for EntityObject class you can try Self-Tracking Entities as per this manual. But as you see it is not advised now... Probably all it'd be required to implement INotifyPropertyChanged interfaces in your entities (they are partial). EntityObject was replaced by the proxy classes. EF generates them for each entity (can be turned off). And includes a tracking there. Hope it helps!

UPDATE. So you can use it to generate validation in entities themselves but that would be some logic or event handling. Usually this is placed into separate modules, into services to "free up" mind. Of course it depends on your specific case, requirements.



来源:https://stackoverflow.com/questions/30817098/the-difference-between-ef6-and-ef4-1-in-files-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!