postgresql (npgsql 2.2.5) with entity framework 6 and code first -


i having issues right initializing database using code first. i'm having problems on how trigger initializer if database not exist.

i've tried following,

https://stackoverflow.com/a/28960111/639713

but problem have call method on initial page trigger create database. that, tables not created unless manually it. issue if i'm going integrate app using sql server , have 50 tables on dbcontext.

anyway, here's code:

dbcontext

public class testmigrationsdatabase : dbcontext     {         public testmigrationsdatabase()             : base(nameorconnectionstring: "testmigrations.domain.entities.testmigrationsdatabase")         {             //database.setinitializer<testmigrationsdatabase>(null);             database.setinitializer<testmigrationsdatabase>(new testmigrations.domain.testmigrationsinitializer());         }         public dbset<base> bases { get; set; }         public dbset<fighter> fighters { get; set; }          protected override void onmodelcreating(dbmodelbuilder modelbuilder)         {             modelbuilder.hasdefaultschema("public"); // postgresql specific             base.onmodelcreating(modelbuilder);         }         public override int savechanges()         {             return base.savechanges();         }     } 

initializer:

public class testmigrationsinitializer : createdatabaseifnotexists<testmigrationsdatabase>     {         protected override void seed(testmigrationsdatabase context)         {              this.createdatabase(configurationmanager.connectionstrings["testmigrations.domain.entities.testmigrationsdatabase"].tostring());              base.seed(context);              loadtesttables(context);         }           private void loadtesttables(testmigrationsdatabase context){             base base = new base();             base.name = "test 1 base";              context.bases.add(base);              context.savechanges();         }          public void createdatabase(string connectionstring)         {             var builder = new npgsqlconnectionstringbuilder(connectionstring);             var databasename = "testmigrations"; // remember original db name             builder.database = "postgres"; // temporarily use postgres database              // create connection database server             using (var connection = new npgsqlconnection(builder.connectionstring))             {                 connection.open();                  // create database                 var createcommand = connection.createcommand();                 createcommand.commandtext = string.format(@"create database ""{0}"" encoding = 'utf8'", databasename);                 createcommand.executenonquery();                  connection.close();             }         }     } 

controller

public class homecontroller : controller     {         public testmigrationsdatabase _context = new testmigrationsdatabase();          //         // get: /home/          public actionresult index()         {             testmigrations.domain.testmigrationsinitializer initializer = new domain.testmigrationsinitializer();             initializer.createdatabase(configurationmanager.connectionstrings["testmigrations.domain.entities.testmigrationsdatabase"].tostring());      return view(); } } 

so that, questions are: 1. putting initializer on first controller trigger correct? or should instantiate context constructor trigger teh initializer? 2. how create tables after database created?

thanks!!!


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -