asp.net mvc 4 - Database initialization strategy Code First -
i have asp.net-mvc
application migration
enabled , simplemembershipprovider
configured use database tables/fields.
my question is, put database initialization? because i'm still developing it, i'm okay dropping database/tables , data , recreate it.
i have put in configuration.cs file, i'm not sure if that's place database initializer.
namespace cfcontext.migrations { using system; using system.data.entity; using system.data.entity.migrations; using system.linq; using webmatrix.webdata; using system.web.security; using system.collections.generic; internal sealed class configuration : dbmigrationsconfiguration<datacontext> { public configuration() { database.setinitializer(new dropcreatedatabasealways<datacontext>()); //database.setinitializer(new dropcreatedatabaseifmodelchanges<datacontext>()); automaticmigrationsenabled = true; automaticmigrationdatalossallowed = true; } protected override void seed(datacontext context) { // method called after migrating latest version. // can use dbset<t>.addorupdate() helper extension method // avoid creating duplicate seed data. e.g. // // context.people.addorupdate( // p => p.fullname, // new person { fullname = "andrew peters" }, // new person { fullname = "brice lambson" }, // new person { fullname = "rowan miller" } // ); // seedmembership(context); } private void seedmembership(datacontext context) { websecurity.initializedatabaseconnection("defaultconnection", "userprofile", "userid", "username", autocreatetables: true); var roles = (simpleroleprovider)roles.provider; var membership = (simplemembershipprovider)system.web.security.membership.provider; if (!roles.roleexists("administrator")) roles.createrole("administrator"); if (membership.getuser("username0", false) == null) { } } } }
public class contextinitializer : createdatabaseifnotexists<context> { private static void initializewebsecurity() { if (websecurity.initialized) return; websecurity.initializedatabaseconnection( connectionstringname: "defaultconnection", usertablename: "user", useridcolumn: "id", usernamecolumn: "email", autocreatetables: true); roles.createrole("admin"); roles.createrole("customer"); } protected override void seed(context context) { initializewebsecurity(); // more seeding context.savechanges(); } } public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); webapiconfig.register(globalconfiguration.configuration); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); authconfig.registerauth(); databaseconfig.registerdatabase(); automapperconfig.registerconfig(); // set initializer here database.setinitializer(new contextinitializer()); // initialize using (var context = new context()) { context.database.initialize(false); } // double check seeding has initalized, if not // initialize here make sure. if (!websecurity.initialized) { websecurity.initializedatabaseconnection( connectionstringname: "defaultconnection", usertablename: "user", useridcolumn: "id", usernamecolumn: "email", autocreatetables: false); } } }
you should able create migrations same way setup.
Comments
Post a Comment