Sergey Kovalenko
Sergey Kovalenko

Reputation: 21

Entity Framework 6. Schema name

I'd like to assign schema name for my entity, without specifying table name. Now, I can do only: modelBuilder.Entity<T>().ToTable("MyEntities", "myschema"); is there a way to do something like: modelBuilder.Entity<T>().ToTable("myschema") ? Please, take into account that I cannot use PluralizationService and calculate tablename manually, since PluralizationService become internal...

Upvotes: 2

Views: 877

Answers (1)

phil soady
phil soady

Reputation: 11328

How about...

var t = typeof (T);
var name= t.Name;
modelBuilder.Entity<T>().ToTable(name, "myschema")

If you need the DbSet plural name from the context

 public DbSet<Single> Plural{ get; set; }

Then this little extension can be reworked to return the value you want. A combo of both without loop. But im sure you will find the right variation...

  public static class BosDalExtensions
{
 public static List<string> GetModelNames(this DbContext context ) {
      var model = new List<string>();
      var propList = context.GetType().GetProperties();
      foreach (var propertyInfo in propList)
      {
      if (propertyInfo.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"))
      {
          model.Add(propertyInfo.Name);

      }
      }


      return model;
  }
 public static List<string> GetModelTypes(this DbContext context)
 {
     var model = new List<string>();
     var propList = context.GetType().GetProperties();
     foreach (var propertyInfo in propList)
     {
         if (propertyInfo.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"   ))
         {
             model.Add(propertyInfo.PropertyType.GenericTypeArguments[0].Name);
         }
     }


     return model;
 }
}

Upvotes: 1

Related Questions