C# 实体框架代码优先-另一个文件中的配置
使用Fluent API分离表到实体的映射的最佳方法是什么,以便它都在一个单独的类中,而不是在OnModelCreating方法中内联 我目前正在做的事情: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( .
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());
}
}