I have been using EF Code First CTP5 with dedicated mapping classes for each entity, like this:
public class UserMapping : EntityTypeConfiguration<User>
{
public UserMapping()
{
ToTable("Users");
HasKey(t => t.ID);
Property(t => t.ID).HasColumnName("user_id");
Property(t => t.Name).HasColumnName("name");
}
}
And loading them the way Jonas Cannehag describes here: http://areaofinterest.wordpress.com/2010/12/08/dynamically-load-entity-configurations-in-ef-codefirst-ctp5/
But that doesn't work in RTM and I haven't been able to figure out how to use dedicated mapping classes. Have you? :-)
public class DataContext : DbContext
{
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserMapping());
base.OnModelCreating(modelBuilder);
}
}
dynamic version (probably should test extensive before putting in production)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister =
Assembly.GetExecutingAssembly().GetTypes().Where(
type =>
type.BaseType.IsGenericType &&
type.BaseType.GetGenericTypeDefinition() == typeof (EntityTypeConfiguration<>));
foreach (object configurationInstance in typesToRegister.Select(Activator.CreateInstance))
{
modelBuilder.Configurations.Add((dynamic) configurationInstance);
}
base.OnModelCreating(modelBuilder);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With