Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架代码优先-另一个文件中的配置_C#_Configuration_Entity Framework 4.1_Code First - Fatal编程技术网

C# 实体框架代码优先-另一个文件中的配置

C# 实体框架代码优先-另一个文件中的配置,c#,configuration,entity-framework-4.1,code-first,C#,Configuration,Entity Framework 4.1,Code First,使用Fluent API分离表到实体的映射的最佳方法是什么,以便它都在一个单独的类中,而不是在OnModelCreating方法中内联 我目前正在做的事情: public class FooContext : DbContext { // ... protected override OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Foo>().Property( .

使用Fluent API分离表到实体的映射的最佳方法是什么,以便它都在一个单独的类中,而不是在OnModelCreating方法中内联

我目前正在做的事情:

public class FooContext : DbContext {
    // ...
    protected override OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Entity<Foo>().Property( ... );
        // ...
    }
}

你是怎么做到的?我使用的是C#。

您需要创建一个从类继承的类,如下所示:

public class FooConfiguration : EntityTypeConfiguration<Foo>
{
    public FooConfiguration()
    {
        // Configuration goes here...
    }
}
详细介绍了如何使用配置类

public class FooConfiguration : EntityTypeConfiguration<Foo>
{
    public FooConfiguration()
    {
        // Configuration goes here...
    }
}
public class FooContext : DbContext
{
    protected override OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new FooConfiguration());
    }
}