c# - Database initializer does not work -
i need create database data loaded. doesn't return error, not workinkg. database created empty. think problem in appconfig
context:
namespace domain.dal { public class landingcontext : dbcontext { public landingcontext() : base(properties.settings.default.constring) { database.setinitializer<landingcontext>(new createdatabaseifnotexists<landingcontext>()); database.initialize(true); } public dbset<package> packages { get; set; } public dbset<absproduct> products { get; set; } } }
initializer:
namespace domain.dal { public class landinginitializer : createdatabaseifnotexists<landingcontext> { protected override void seed(landingcontext context) { car kangoo = new car(100, 2, "kangoo"); context.products.add(kangoo); context.savechanges(); base.seed(context); } } }
and appconfig:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configsections> <!-- more information on entity framework configuration, visit http://go.microsoft.com/fwlink/?linkid=237468 --> <section name="entityframework" type="system.data.entity.internal.configfile.entityframeworksection, entityframework, version=6.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false" /> </configsections> <connectionstrings> <add name="domain.properties.settings.constring" connectionstring="data source=enzo-pc;initial catalog=glb-landing;integrated security=true" providername="system.data.sqlclient" /> </connectionstrings> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5.2" /> </startup> <entityframework> <defaultconnectionfactory type="system.data.entity.infrastructure.localdbconnectionfactory, entityframework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultconnectionfactory> <providers> <provider invariantname="system.data.sqlclient" type="system.data.entity.sqlserver.sqlproviderservices, entityframework.sqlserver" /> </providers> </entityframework> <contexts> <context type=" dal.landingcontext, domain"> <databaseinitializer type="dal.landinginitializer, domain" /> </context> </contexts> </configuration>
i reeplace
database.setinitializer<landingcontext>(new createdatabaseifnotexists<landingcontext>());
for:
database.setinitializer<landingcontext>(new landinginitializer());
and work...
Comments
Post a Comment