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
Post a Comment