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()
inEF6
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: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, addDataMember
attribute to generated classes or some other changes). That's why it's better to useT4 templates. These are files with
.tt
extension. All they do is just execute their logic whenRun 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. IfT4
- 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 useLegacy 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 yourOnPropertyChanged
andOnPropertyChanging
back, because they are the methods ofEntityObect
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 derivedDbContext
in it withDbSets
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.OnPropertyChanged
- that should be just an implementation ofINotifyPropertyChanged
interface. However looks like you are using template that generate POCO classes. That is the default and recommended option, but to get the implementation ofINotifyPropertyChanged
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.