Abaixo um exemplo de como carregar as configurações customizadas de mapeamentos para o Entity Framework Code First.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
//MyDataContext: is my current database context | |
var configurationTypes = typeof(DataContext).Assembly.GetTypes() | |
.Where(t => t.IsAbstract == false && | |
t.BaseType != null && | |
t.BaseType.IsGenericType && | |
(t.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>) || | |
t.BaseType.GetGenericTypeDefinition() == typeof(ComplexTypeConfiguration<>))) | |
.ToArray(); | |
foreach (var configurationType in configurationTypes) | |
{ | |
dynamic configurationTypeInstance = Activator.CreateInstance(configurationType); | |
modelBuilder.Configurations.Add(configurationTypeInstance); | |
} | |
base.OnModelCreating(modelBuilder); | |
Database.SetInitializer<DataContext>(null); | |
} |
Nenhum comentário:
Postar um comentário