实体框架代码首先允许在单独的文件中流畅的映射? [英] Does Entity Framework Code First allow for fluent mappings in separate files?

查看:73
本文介绍了实体框架代码首先允许在单独的文件中流畅的映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework Code First开发一个相当大的数据库模式。我喜欢通过数据注释方法的Fluent API,因为它将我的域对象作为简单的POCO。



为了使用Fluent API,我必须覆盖OnModelCreating从DbContext继承的类。



我不喜欢所有的实体的所有映射都在这种方法中。我以前使用过FluentNHibernate,每个实体都有它自己的映射类。 EF有什么类似的吗?



我想我可以创建自己的接口来实现映射类,并在OnModelCreating方法中调用它们。我可以使用反射或IoC来发现它们。我没有特别看到这种方法有什么问题,但是我想知道实体框架是否已经附带这样的东西?

解决方案

您可以从 EntityTypeConfiguration< TEntity> 派生的实体创建一个配置类,并将这些类放入单独的文件中。在派生的 DbContext 中,将这些配置类的实例添加到模型构建器。示例:

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

属性(u => u.UserName)
.HasMaxLength(50)
.IsRequired();

//等
}
}

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

//等
}
}

派生上下文:

  public class MyContext:DbContext 
{
// ...

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

还有一个 ComplexTypeConfiguration< ; T> 您可以从中配置复杂类型。


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?

解决方案

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.

这篇关于实体框架代码首先允许在单独的文件中流畅的映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆