Difference between EF6 and EF4.1 in file hierarchy

I am starting to work with Entity Framework.

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

I have a .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 this code:

  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 a .tt file with a .cs file for each object, for example Customer.cs

With this code:

  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 is completely different if I use EF4.1 with visual studio 2010 , there is only one code per .Designer.cs file for the model !!


  • Can someone help me understand what all these files are for .Context.tt , .Context.cs , .tt , .cs ? and which are different in the file hierarchy between the two cases (EF6,EF4.1) ?
  • I can not find OnPropertyChanging(Value) & OnPropertyChanged() in EF6 to check my entities !! Why do these methods no longer exist and how to check my objects? properties if they do not exist?
+6
source share
2 answers

So let's clarify step by step:

  • You have your .edmx file that was created by the designer or created from an existing database. But this is only an XML file that contains information about the database structure that is used - the storage scheme , information about objects - the conceptual scheme and the mapping between the two. It does not contain executable code. This code needs to be created.

  • To generate the code, the .edmx file will be processed, and .cs files containing the actual executable code will be created. You can use 2 approaches:

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

    • T4 templates . These are files with the extension .tt . All they do is simply execute their logic when the Run custom tool is selected in the context menu or the .edmx file is .edmx . There is a set of templates available for generating EF code from .edmx , some information here . And since these are regular files, you can change them as needed (to use the editor better, use the T4 material extension ). Some basic information on using T4 templates in EF is here .

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

enter image description here

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

Another difference between the two is that the 1st generates obsolete ObjectContext and entities derived from EntityObect . They will all be in this .Designer.cs file. But this is not recommended anymore (however, you can still get the appropriate T4 template - this way you will get your OnPropertyChanged and OnPropertyChanging back, because they are methods of the EntityObect class, however they are protected, so you may need to write some wrappers). But it's better to use the POCO classes and the DbContext template - the one used by VS 2013 in your case. Then you will get a separate .Context.tt for generating .Context.cs with a derived DbContext in it with DbSets representing tables, and a .tt file for creating entity classes. The hierarchy between .tt and .cs only shows which .cs were generated using .tt , while only .cs will actually execute and execute when your application starts.

  • And now with respect to OnPropertyChanged - it should just be an implementation of the INotifyPropertyChanged interface. However, it looks like you are using a template that generates POCO classes. This is a standard and recommended option, but in order to implement INotifyPropertyChanged you may need to edit the template or choose another one from the Visual Studio Online gallery. This, however, may not be the best architectural solution, because sometimes it is better to highlight the entities and classes that you use for UI / business logic.
+7
source

*.tt files T4 templates used to auto- .EDMX C # code from an .EDMX file . This is basically your storage scheme, conceptual scheme, and mapping between them. .context.cs is your DbContext (formerly ObjectContext ), and others are entities. The tool used to generate the code is EntityModelCodeGenerator , as seen from the properties of the .EDMX file. So it's pretty straight forward.

To get OnPropertyChanged , which was for the EntityObject class, you can try Self-Tracking Entities in accordance with this guide . But, as you can see, now this is not recommended ... Probably, all this would require the implementation of INotifyPropertyChanged interfaces in your entities (they are partial ). EntityObject been replaced by proxy classes. EF generates them for each object (can be disabled). And includes tracking there. Hope this helps!

UPDATE Thus, you can use it to generate validation in the entities themselves, but this will be some logic or event handling. Usually this is placed in separate modules, in services to β€œfree” the mind. Of course, it depends on your specific case, requirements.

+3
source

Source: https://habr.com/ru/post/989055/


All Articles