【问题标题】:Set database schema name without setting table name设置数据库模式名称而不设置表名
【发布时间】:2015-09-13 09:29:15
【问题描述】:

我想为表设置数据库模式名称,但唯一的方法是使用方法ToTable

modelBuilder.Entity<MyEntity>().ToTable("MyTable", schemaName);

但是我不想明确设置表名,我需要这样的东西:

modelBuilder.Entity<MyEntity>().ToSchema(schemaName);

谁能帮帮我?

【问题讨论】:

  • 你能详细说明一下动机吗?目前尚不清楚如果没有表名,实体将如何使用
  • 我写了'显式',隐式表名将被设置。
  • 自定义约定抛开,会不会和 ToTable(typeof(MyEntity).Name, schemaName) 一样?
  • @jbl 不,表名不一定与类型名匹配(例如复数形式)

标签: c# entity-framework entity-framework-6 database-schema


【解决方案1】:

使用此扩展方法:

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2013-04-22
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多