Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Entity Framework Code First allow for fluent mappings in separate files?

I am developing a rather large database schema using Entity Framework Code First. I prefer the Fluent API over the Data Annotations approach, as it leaves my domain objects as simple POCOs.

In order to use Fluent API, I have to override OnModelCreating in the class that inherits from DbContext.

I don't like that all mappings for all of my entities are in this one method. I have used things like FluentNHibernate before, where each entity has it's own mapping class. Does EF have anything similar?

I suppose I could create my own interface to implement a mapping class and call them all within the OnModelCreating method. I could use reflection or an IoC to discover them all. I don't particularly see anything wrong with this approach, but I was wondering if Entity Framework already comes with something like this out of the box?

like image 513
Matt Johnson-Pint Avatar asked Nov 30 '11 00:11

Matt Johnson-Pint


People also ask

What is mapping in Entity Framework?

Mapping: Mapping consists of information about how the conceptual model is mapped to the storage model. LINQ to Entities: LINQ-to-Entities (L2E) is a query language used to write queries against the object model.

What is Fluent API in and how is it different from data annotations?

Fluent API is an advanced way of specifying model configuration that covers everything that data annotations can do in addition to some more advanced configuration not possible with data annotations.

Which of the following configuration activities Fluent API does?

Fluent API configures the following aspect of a model in Entity Framework 6: Model-wide Configuration: Configures the default Schema, entities to be excluded in mapping, etc.

Which are the correct Fluent API methods for describing relationships?

When configuring a relationship with the fluent API, you start with the EntityTypeConfiguration instance and then use the HasRequired, HasOptional, or HasMany method to specify the type of relationship this entity participates in.


1 Answers

You can create one configuration class per entity derived from EntityTypeConfiguration<TEntity> and put these classes into separate files. In your derived DbContext you add instances of those configuration classes to the model builder. Example:

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        HasKey(u => u.UserName);

        Property(u => u.UserName)
            .HasMaxLength(50)
            .IsRequired();

        // etc.
    }
}

public class RoleConfiguration : EntityTypeConfiguration<Role>
{
    public RoleConfiguration()
    {
        HasKey(r => r.RoleName);

        // etc.
    }
}

Derived context:

public class MyContext : DbContext
{
    //...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserConfiguration());
        modelBuilder.Configurations.Add(new RoleConfiguration());
    }
}

There is also a ComplexTypeConfiguration<T> you can derive from to configure complex types.

like image 175
Slauma Avatar answered Oct 18 '22 19:10

Slauma