我想为表设置数据库架构名称,但唯一的方法是使用方法 ToTable
:
modelBuilder.Entity<MyEntity>().ToTable("MyTable", schemaName);
但是我不想显式设置表名,我需要这样的东西:
modelBuilder.Entity<MyEntity>().ToSchema(schemaName);
有人可以帮我吗?
最佳答案
使用这个扩展方法:
using System.Data.Entity.ModelConfiguration;
public static class ModelConfigurationHelper
{
public static EntityTypeConfiguration<T> ToSchema<T>(this EntityTypeConfiguration<T> config, string schema)
{
return config.ToTable(typeof(T).Name, schema);
}
}
你可以这样做:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().ToSchema("someSchema);
}
关于c# - 设置数据库模式名称而不设置表名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31072970/