如何动态地将实体框架模型映射到表名

Bui*_*ted 20 entity-framework entity-framework-4

使用代码优先方法我想动态地将单个模型映射到多个表名.目前我可以做类似的事情modelBuilder.Entity(Of Person)().MapSingleType().ToTable("Managers"),因为OnModelCreating只有在我无法动态地将其映射到其他表名时才调用该方法.

在我们当前的LinqToSql版本中,我们将覆盖MetaModel.GetTable()并使用我们的动态名称返回一个新的TableAttribute.我没有在EF中找到这样的属性(即使我不知道如何覆盖它).所以我的问题是:是否可以这样做(还)?

更新

我发现我可以OnModelCreating通过调用阻止该方法缓存映射

modelBuilder.CacheForContextType = false;
Run Code Online (Sandbox Code Playgroud)

因此,我可以在对象的实例化上分配表定义.这不是我想要的方式,但它有效.

更新

哦,男孩,上面是一个大错误......缓存存在是有原因的!:)所以我回到POCO对象映射的方块.如果我找到解决方案,我会发布更新.

最后

如果有人关心我目前如何解决这个问题,请点击此处:

首先,我使用POCO表和接口创建了一个单独的库

public interface IDataContext {
    System.Data.Entity.DbSet<TableGeneric> TableGeneric { get; set; }

    int SaveChanges();
}


public class TableGeneric {
    [Key]
    public int Column1 { get; set; }
    public string Column2 { get; set; }
    public DateTime Column3 { get; set; }
    public string Column4 { get; set; }
    public string Column5 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后,使用CSharpCodeProvider我创建了一个类,它接受以下模板并将其转换为类型定义:

class DataContext : System.Data.Entity.DbContext, IDataContext {
    public System.Data.Entity.DbSet<TableGeneric> TableGeneric { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder
            .Entity<ContextTesting.Interfaces.EF.TableGeneric()
                .MapSingleType()
                .ToTable("$TableName$");
    }
}
Run Code Online (Sandbox Code Playgroud)

使用生成的类型,我可以创建一个实例,所以我们在这里

Type typeAccountants = BuildContext.CreateGenericTable("Accountants");
IDataContext context = (IDataContext)Activator.CreateInstance(typeAccountants);
Run Code Online (Sandbox Code Playgroud)

然后剩下的就像你有一个普通的DataContext.希望这有助于其他人.

Bui*_*ted 13

如果有人关心我目前如何解决这个问题,请点击此处:

首先,我使用POCO表和接口创建了一个单独的库

public interface IDataContext {
    System.Data.Entity.DbSet<TableGeneric> TableGeneric { get; set; }

    int SaveChanges();
}


public class TableGeneric {
    [Key]
    public int Column1 { get; set; }
    public string Column2 { get; set; }
    public DateTime Column3 { get; set; }
    public string Column4 { get; set; }
    public string Column5 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后,使用CSharpCodeProvider我创建了一个类,它接受以下模板并将其转换为类型定义:

class DataContext : System.Data.Entity.DbContext, IDataContext {
    public System.Data.Entity.DbSet<TableGeneric> TableGeneric { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder
            .Entity<ContextTesting.Interfaces.EF.TableGeneric()
                .MapSingleType()
                .ToTable("$TableName$");
    }
}
Run Code Online (Sandbox Code Playgroud)

使用生成的类型,我可以创建一个实例,所以我们在这里

Type typeAccountants = BuildContext.CreateGenericTable("Accountants");
IDataContext context = (IDataContext)Activator.CreateInstance(typeAccountants);
Run Code Online (Sandbox Code Playgroud)

然后剩下的就像你有一个普通的DataContext.希望这有助于其他人.

  • 这已过期,不再有效,@FooBar。从那以后,实体框架已经进行了多次修订。 (3认同)