Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between EF6 and EF4.1 in files hierarchy

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?
like image 444
Anyname Donotcare Avatar asked Jun 13 '15 09:06

Anyname Donotcare


People also ask

What is the difference between EF6 and EF core?

EF 6 is a stable and mature ORM while EF Core is relatively new. Microsoft rebuilt EF Core from the ground up and removed many of the internal dependencies and providers that EF 6 had (like SQLClient). In the long run, that will make EF Core much more extensible and lighter weight.

What is difference between Entity Framework 5 and 6?

EF5 is built into the core of . NET 4.5, whereas EF6 has been shifted out, and is open source. This means that you must add the new EF6 assemblies to all of the relevant projects in the solution, in particular the entry project. This means that you must remove assembly System.

Is EF core faster than EF6?

Entity Framework (EF) Core, Microsoft's object-to-database mapper library for . NET Framework, brings performance improvements for data updates in version 7, Microsoft claims. The performance of SaveChanges method in EF7 is up to 74% faster than in EF6, in some scenarios.

What are the three approaches in Entity Framework?

There are three approaches to model your entities in Entity Framework: Code First, Model First, and Database First. This article discusses all these three approaches and their pros and cons.


1 Answers

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:

enter image description here

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.
like image 189
Pavel K Avatar answered Oct 16 '22 07:10

Pavel K