c# - EF DBConfiguration associated automatically to DBContext without annotation -
using latest ef (6.1.1) , ef sql server compact (6.1.1) pulls in compact 4.0. target net 4.5.1
looking code ef , later on piece structuremap. created dbconfiguration class:
public class myconfiguration : dbconfiguration { setdatabaseinitializer<mycontext>(new myinitializer()); setproviderservices(sqlceproviderservices.providerinvariantname, sqlceproviderservices.instance); }
and context:
public class mycontext : dbcontext { public mycontext(string connection) : base(connection) {} .... }
with xunit run:
using (var ctx = new mycontext(string.format("data source={0}", path.combine(databasepath, databasename)))) { ctx.database.initialize(true) }
the test case creates (and seeds through initializer) compact database. there bunch of implicit stuff happening behind scenes don't get.
for example, don't need have following annotation on mycontext class:
[dbconfigurationtype(typeof(myconfiguration))]
ef must smart enough see have myconfiguration class extends dbconnection , use it. or? can remove myconfiguration , test case still generate database (obviously not seeded). how?
i love able code assign dbconfiguration specific dbcontext without going through static:
static mycontext() { dbconfiguration.set(new myconfiguration()); }
reason being different "profiles" structuremap. ideas?
yes , no in web.config should have section this
<contexts> <context type="<dal.mycontext, dal"> <databaseinitializer type="dal.myconfiguration, dal" /> </context> </contexts>
where dal
mycontext
, myconfiguration
exist
[edit]
to in code must use static method dbconfiguration.setconfiguration(dbconfiguration configuration)
per here , must set before use of context.
Comments
Post a Comment