Set database schema name without specifying table name
I want to set the database schema name for a table, but the only way is to use the method ToTable
:
modelBuilder.Entity<MyEntity>().ToTable("MyTable", schemaName);
However, I don't want to explicitly specify the table name, I need something like this:
modelBuilder.Entity<MyEntity>().ToSchema(schemaName);
Can anyone help me?
+3
source to share
1 answer
Using this extension method:
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);
}
}
You can do:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().ToSchema("someSchema);
}
-1
source to share