четверг, 23 августа 2012 г.

Entity Framework and the missing class EntityConfiguration


Many blogs were written with EntityConfiguration class which doesn’t exist in EF4.1 release version and above.
If you need to do the mapping of your properties with table columns, you should not useEntityTypeConfiguration or ComplexTypeConfiguration class. Those classes are in the namespace “System.Data.Entity.ModelConfiguration”.
This will let you in your DbContext class to override the method “OnModelCreating” and to set the configuration desired.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   //...
   modelBuilder.Configurations.Add(new MyClassConfiguration());
   base.OnModelCreating(modelBuilder);
    
}       
 
//In an other class:
public class MyClassConfiguration: EntityTypeConfiguration
{
     public MyClassConfiguration()
     {
            ToTable("MySuperOtherNameTable");
            Property(c => c.Code).HasColumnName("Code_Col1");
            Property(c => c.Name"Name_FR_CANADA");
     }
}
This let you have Entity Framework on a database that has been constructed without having the same name of your model. This use the Fluent interface of Entity Framework to accomplish this task. EF let you do it by attribute also (this will be discussed later). Of course, this require more manual labour but can be great to by pass some objects, one object or even a part of an object. Indeed, this can be use only on few properties of your object and let Entity Framework handle the rest of your object automatically.

Комментариев нет: