在实体框架 4.1 中通用定义数据库模式名称

发布于 2024-11-25 06:38:58 字数 333 浏览 3 评论 0原文

我知道您可以使用 ToTable("TableName", "SchemaName") 定义每个类的实体架构名称,但是有没有办法进行设置,以便您可以为中的所有表设置架构名称配置,因为当我使用某些类型的关系映射和拆分实体映射时,我得到一些奇怪的结果,它在内部 sql 查询中恢复为默认的 dbo.TableName

请参阅 这篇较早的文章获取 SQL 输出示例

i know you can define the entity's schema name per class by using ToTable("TableName", "SchemaName") but is there a way to set it up so you can set the schema name for all tables in the configuration as i am getting some weird results when i am using some types of relationship mapping and split entity mapping where it is reverting back to the default dbo.TableName in the internal sql queries

see this earlier post for sql output example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风情万种。 2024-12-02 06:38:58

我使用 EF 4.1 优先使用 Oracle 数据库,所有映射均通过数据注释完成。测试和生产环境中的架构名称不同。我的解决方案是在 OnModelCreating 期间借助 Fluent API、反射和后期绑定动态映射架构。迭代泛型类型的所有 Context 类属性并完成脏工作。到目前为止对我有用。

public class Context : DbContext
{
    public Context()
        : base(new OracleConnection(ConfigurationManager.ConnectionStrings["OraAspNetConString"].ConnectionString), true)
    {
    }

    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        foreach (var p in typeof(Context).GetProperties().Where(foo=>foo.PropertyType.IsGenericType))
        {
            // this is what we are trying to accomplish here -- 
            //modelBuilder.Entity<User>().ToTable("TBL_USERS", "TestSchema");

            Type tParam = p.PropertyType.GetGenericArguments()[0]; // typeof User
            MethodInfo generic = typeof(DbModelBuilder).GetMethod("Entity").MakeGenericMethod(new[] { tParam });
            object entityTypeConfig = generic.Invoke(modelBuilder, null);

            MethodInfo methodToTable = typeof(EntityTypeConfiguration<>).MakeGenericType(new[] { tParam }).GetMethod("ToTable", new Type[] { typeof(string), typeof(string) });
            methodToTable.Invoke(entityTypeConfig, new[] { GetMappedTableName(tParam), currentOraSchemaName });
        }

        base.OnModelCreating(modelBuilder);
    }

    private string currentOraSchemaName = ConfigurationManager.AppSettings.Get("OraSchemaName");

    private string GetMappedTableName(Type tParam)
    {
        TableAttribute tableAttribute = (TableAttribute)tParam.GetCustomAttributes(typeof(TableAttribute), false).FirstOrDefault();
        return tableAttribute.Name;
    }
}

这里的用户类,没有硬编码的模式映射——

[Table("TBL_USERS")]
public class User
{
    [Column("USER_ID")]
    public string UserId { get; set; }

    [Column("USER_NAME")]
    public string Name { get; set; }} 

Im having Oracle database-first with EF 4.1, all mappings done with Data Annotations. Different Schema names in Test and Production environments. My solution is to map the Schema dynamically during OnModelCreating with some help of fluent API, reflection and late binding. Iterate through all Context class properties of generic type and do the dirty work. Works for me so far.

public class Context : DbContext
{
    public Context()
        : base(new OracleConnection(ConfigurationManager.ConnectionStrings["OraAspNetConString"].ConnectionString), true)
    {
    }

    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        foreach (var p in typeof(Context).GetProperties().Where(foo=>foo.PropertyType.IsGenericType))
        {
            // this is what we are trying to accomplish here -- 
            //modelBuilder.Entity<User>().ToTable("TBL_USERS", "TestSchema");

            Type tParam = p.PropertyType.GetGenericArguments()[0]; // typeof User
            MethodInfo generic = typeof(DbModelBuilder).GetMethod("Entity").MakeGenericMethod(new[] { tParam });
            object entityTypeConfig = generic.Invoke(modelBuilder, null);

            MethodInfo methodToTable = typeof(EntityTypeConfiguration<>).MakeGenericType(new[] { tParam }).GetMethod("ToTable", new Type[] { typeof(string), typeof(string) });
            methodToTable.Invoke(entityTypeConfig, new[] { GetMappedTableName(tParam), currentOraSchemaName });
        }

        base.OnModelCreating(modelBuilder);
    }

    private string currentOraSchemaName = ConfigurationManager.AppSettings.Get("OraSchemaName");

    private string GetMappedTableName(Type tParam)
    {
        TableAttribute tableAttribute = (TableAttribute)tParam.GetCustomAttributes(typeof(TableAttribute), false).FirstOrDefault();
        return tableAttribute.Name;
    }
}

The user class here, with no hard-coded schema mapping --

[Table("TBL_USERS")]
public class User
{
    [Column("USER_ID")]
    public string UserId { get; set; }

    [Column("USER_NAME")]
    public string Name { get; set; }} 
沩ん囻菔务 2024-12-02 06:38:58

由于最终 EFv4.1 版本没有用于自定义约定的公共 API,因此您无法从 API 全局更改架构。

Since final EFv4.1 version doesn't have public API for custom conventions you cannot change the schema globally from the API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文