c# - How to choose DbConfigurationType programatically? -


i have dbcontext:

[dbconfigurationtype(typeof(mysqlefconfiguration))] public class mydbcontext : dbcontext {     public dbset<myclass> myclasses { get; set; } } 

now, if want able use dbconfigurationtype, according appsettings value?

so that, change appsettings value mysql mssql , becomes:

[dbconfigurationtype(typeof(mssqlefconfiguration))] public class mydbcontext : dbcontext {     public dbset<myclass> myclasses { get; set; } } 

of course, can create factory, , use reflection create new class custom attribute. however, there solution without reflection?

i have solved problem inheriting dbconfigurationtypeattribute:

public class baseefconfiguration : dbconfigurationtypeattribute {     public baseefconfiguration() : base(sqlconfiguration)     {     }      public static type sqlconfiguration     {                 {             string databasetypename = configurationmanager.appsettings["databasetype"];             switch (databasetypename)             {                 case "mysql":                     return typeof(mysqlefconfiguration);                 case "mssql":                     return typeof(mssqlefconfiguration);                 default:                     throw new notimplementedexception($"no such sql configuration type {databasetypename}");             }         }     } }  [baseefconfiguration] public class basedbcontext : dbcontext {  } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

android - CoordinatorLayout, FAB and container layout conflict -