简体   繁体   中英

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. 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:

[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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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