Guest User

Untitled

a guest
Jul 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public class Context : DbContext
  2. {
  3. public Context()
  4. : base(new OracleConnection(ConfigurationManager.ConnectionStrings["OraAspNetConString"].ConnectionString), true)
  5. {
  6. }
  7.  
  8. public DbSet<User> Users { get; set; }
  9.  
  10. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  11. {
  12. foreach (var p in typeof(Context).GetProperties().Where(foo=>foo.PropertyType.IsGenericType))
  13. {
  14. // this is what we are trying to accomplish here --
  15. //modelBuilder.Entity<User>().ToTable("TBL_USERS", "TestSchema");
  16.  
  17. Type tParam = p.PropertyType.GetGenericArguments()[0]; // typeof User
  18. MethodInfo generic = typeof(DbModelBuilder).GetMethod("Entity").MakeGenericMethod(new[] { tParam });
  19. object entityTypeConfig = generic.Invoke(modelBuilder, null);
  20.  
  21. MethodInfo methodToTable = typeof(EntityTypeConfiguration<>).MakeGenericType(new[] { tParam }).GetMethod("ToTable", new Type[] { typeof(string), typeof(string) });
  22. methodToTable.Invoke(entityTypeConfig, new[] { GetMappedTableName(tParam), currentOraSchemaName });
  23. }
  24.  
  25. base.OnModelCreating(modelBuilder);
  26. }
  27.  
  28. private string currentOraSchemaName = ConfigurationManager.AppSettings.Get("OraSchemaName");
  29.  
  30. private string GetMappedTableName(Type tParam)
  31. {
  32. TableAttribute tableAttribute = (TableAttribute)tParam.GetCustomAttributes(typeof(TableAttribute), false).FirstOrDefault();
  33. return tableAttribute.Name;
  34. }
  35. }
  36.  
  37. [Table("TBL_USERS")]
  38. public class User
  39. {
  40. [Column("USER_ID")]
  41. public string UserId { get; set; }
  42.  
  43. [Column("USER_NAME")]
  44. public string Name { get; set; }}
Add Comment
Please, Sign In to add comment