asp.net mvc - EF not seeding database on creation -
i using entity framework create , seed database using code first , migratedatabasetolatestversion initializer. issue having when launch asp.net mvc app without database create ef create database not seed on first run through. if kill iisexpress , relaunch app after creating database seeds go in fine. expect seeds ran after database gets created don't nail break point in seeds method on first run through. nail break points on sec run through without problems annoying have run app twice after killing db seeds work.
below configuration class:
public sealed class configuration : dbmigrationsconfiguration<compassdb> { public configuration() { automaticmigrationsenabled = true; automaticmigrationdatalossallowed = true; } protected override void seed(compassdb context) { moduleseed.seed(context); permissiongroupseed.seed(context); var permissions = permissionseed.seed(context); var roles = roleseed.seed(context, permissions); userseed.seed(context, roles, permissions); ocmpluginseed.seed(context); scacseed.seed(context); moduleconfigurationseed.seed(context); } }
i calling in global.asax file.
database.setinitializer(new migratedatabasetolatestversion<compassdb, configuration>()); using (var db = new compassdb()) { db.database.initialize(true); }
i did have query version number db on page load create database calling initializer straight seems little cleaner. having issue before when making db phone call through ef well. moved away db phone call because using ef automatic db creating , migration switch dapper database communication.
i found post here people having same issue me doesn't seem ever resolved.
updatei found out issue related migration files. updated primary keys of models int long , had delete current migration files. after deleting files started working normal, database created , seeded on same request. created initial schema migration , @ same issue database not seed until 2nd time launching site. using elmah in project , have update first migration file execute sql file included when installing elmah via nuget. related issue , more testing see if cause.
i think had same or similar problem. seek create manual initializer. it's clean, simple , short. see example:
public class customdbinit implements idatabaseinitializer(of mycontext) public sub initializedatabase(context mycontext) implements system.data.entity.idatabaseinitializer(of mycontext).initializedatabase if not context.database.exists context.database.createifnotexists() ' other processes, such webmatrix initialization if you're using simplemembership end if end sub end class
then on global.asax
application_start method, initialize this:
dim context new mycontext() if not context.database.exists database.setinitializer(new customdbinit()) context.database.initialize(false) end if
asp.net-mvc entity-framework code-first-migrations
No comments:
Post a Comment