c# - 设置数据库模式名称而不设置表名称

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

我想为表设置数据库架构名称,但唯一的方法是使用方法 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/

相关文章:

c# - 使用 Fluentvalidation 显示带有 NotEmpty 的星号

c# - Entity Framework 在请求结束之前无法读取新数据

c# - 因为它有一个 DefiningQuery 并且 <ModificationFunctionMapping> 元素中不存在 <InsertFunction> 元素

entity-framework - 在 EF Core 中自动设置每条记录的创建和修改日期的问题

c# - Entity Framework 6 迁移是否可以包含围绕脚本的事务?

mysql - 如何在 MySql 表中存储 MS Sql Server 唯一标识符

c# - 将 .Net Core 构建为 EXE 而不是 DLL

c# - Outlook 2010 - C# - 获取与邮件关联的帐户

c# - 在 ViewModel 中具有静态 DbContext 的 WPF 应用程序

c# - 用集合EntityFramework连接表