简体   繁体   English

首先在不同架构中映射具有相同表的实体 Entity Framework 6 代码

[英]Map entity with same table in different schemas Entity Framework 6 code first

I apologize if similar questions exists, but cannot find exactly what I want.如果存在类似问题,我深表歉意,但找不到我想要的。

I am using Entity Framework 6 Code First.我正在使用 Entity Framework 6 Code First。 I have database with different schemes.我有不同方案的数据库。 Each schema is mapped on different user and tables are same cross schemes.每个模式映射到不同的用户,表是相同的交叉模式。 I created Entity Framework 6 Code First and map entity with table by using attribute like in following example:我创建了 Entity Framework 6 Code First 并使用如下示例中的属性映射实体与表:

[Table("Log")]
public partial class Log
{
    /// <summary>
    /// Id
    /// </summary>
    public int Id { get; set; }
    /// <summary>
    /// Message
    /// </summary>
    [Required]
    public string Message { get; set; }
}

This example above doesn't work because I am not using default schema and I have multiple schemes.上面的这个例子不起作用,因为我没有使用默认模式并且我有多个方案。 If I include schema name in attribute eg like this如果我在属性中包含模式名称,例如像这样

    [Table("SHEMA_NAME.Log")]

it will works.它会起作用。

I know that I can solve my problem programmatically supplying schema name during model creation.我知道我可以在模型创建期间以编程方式提供模式名称来解决我的问题。

But is there any way to use some generic way to map entity with all tables from different schemes without specifying schema name?但是有没有办法使用某种通用方式将实体与来自不同方案的所有表进行映射而不指定模式名称?

Thanks谢谢

In your Context you can specify a DefaultSchema:在您的上下文中,您可以指定一个 DefaultSchema:

public class YourContext: DbContext 
{
    public YourContext(): base() 
    {
    }

    public DbSet<Log> Logs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Configure default schema
        modelBuilder.HasDefaultSchema("SHEMA_NAME");
    }
}

See: http://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspx请参阅: http : //www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM