The difference between EF6 and EF4.1 in files hierarchy

c# edmx entity-framework entity-framework-6 visual-studio-2012


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...'. 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.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?
6/23/2015 9:25:54 AM

Accepted Answer

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 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.
6/19/2015 2:33:50 PM

Popular Answer

*.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.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow