ajr ajr - 16 days ago 5
C# Question

Entity-framework-7 Organizing Fluent API configurations into a separate class

I'm familiar how to organize the fluent API configurations into a separate class on EF6, but how is this achieved with EF7?

Here is an example how to do it with EF6:

ModelConfigurations.cs

public class ModelConfigurations : EntityTypeConfiguration<Blog>
{
ToTable("tbl_Blog");
HasKey(c => c.Id);
// etc..
}


and to call it from OnModelCreating()

protected override void OnModelCreating(DbModelbuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ModelConfigurations());
// etc...
}


On EF7 I cant resolve the EntityTypeConfiguration? What is the correct way to implement fluent API calls from a separate class?

Answer

Try this:

public class BlogConfig
{
    public BlogConfig(EntityTypeBuilder<Blog> entityBuilder)
    {
        entityBuilder.HasKey(x => x.Id);
        // etc..
    }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    new BlogConfig(modelBuilder.Entity<Blog>());
}