c# - Entity Framework 5 code-first not creating database -


i'm trying create new database using code first concept of entity framework. when running code database isn't created (using dropcreatedatabaseifmodelchanges setting), though code running fine. i'm seeing following exception when try database.

enter image description here

my project setup using separate dataaccess layer generic service , repository construction. entities, repository , database context in separate project within solution.

my global.asax file contains following piece of code.

database.setinitializer(new dropcreatedatabaseifmodelchanges<mycontext>()); 

this should initialise new database if isn't there, right?

my database context class looks this;

namespace website.dal.model {     public class mycontext : dbcontext     {         public idbset<project> projects { get; set; }         public idbset<portfolio> portfolios { get; set; }          /// <summary>         /// constructor, provide connectionstring used it's base class.         /// </summary>         public mycontext()             : base("myconnectionstring")         {         }          static mycontext()         {             try             {                 database.setinitializer<mycontext>(new dropcreatedatabaseifmodelchanges<mycontext>());             }             catch (exception)             {                 throw;             }         }          /// <summary>         /// method prevents plurarization of table names         /// </summary>         /// <param name="modelbuilder"></param>         protected override void onmodelcreating(dbmodelbuilder modelbuilder)         {             base.onmodelcreating(modelbuilder);             modelbuilder.conventions.remove<system.data.entity.modelconfiguration.conventions.pluralizingtablenameconvention>();         }     } } 

i've created class following several tutorials , articles on internet. it's new me, far can see seems correct far. 2 entities i'm using. they're called 'project' , 'portfolio'. this;

public class portfolio     {         [key]         public guid id { get; set; }         public string name { get; set; }         public datetime startdate { get; set; }         public datetime? enddate { get; set; }         public bool ispublished { get; set; }          public virtual icollection<project> projects { get; set; }     } 

and

public class project      {         [key]         public guid id { get; set; }         public datetime startdate { get; set; }         public datetime? enddate { get; set; }         public bool ispublished { get; set; }         public string title { get; set; }     } 

the database i'm using running on external server, came hosting provider i'm using. i've got sql server database , running , connection string database in web.config of website project. i've tried removing database , let code recreate it, unfortunately didn't work. missing obvious here? or simple thing access-rights server create databases?

note: when run database-update -script command generate sql code, seems correct sql statements create tables created.

update 1: okay, comments came bit further. i've added 2 properties entities force changes , i've created custom initializer this;

public class forcedeleteinitializer : idatabaseinitializer<mycontext>     {         private readonly idatabaseinitializer<mycontext> _initializer = new dropcreatedatabaseifmodelchanges<mycontext>();          public forcedeleteinitializer()         {             //_initializer = new forcedeleteinitializer();         }          public void initializedatabase(mycontext context)         {             //this command added prevent open connections. see http://stackoverflow.com/questions/5288996/database-in-use-error-with-entity-framework-4-code-first             context.database.executesqlcommand("alter database borloontwikkel set single_user rollback immediate");             _initializer.initializedatabase(context);         }     } 

i've removed initializer constructor of context, mean i've remove line of code;

database.setinitializer<mycontext>(new dropcreatedatabaseifmodelchanges<mycontext>()); 

after i've added these 3 lines global.asax file;

database.setinitializer(new forcedeleteinitializer()); mycontext c = new mycontext(); c.database.initialize(true); 

when debugging i'm getting exception; enter image description here

this gives me following information:

  • innerexception says: provider didn't return providermanifesttoken
  • innerexception in innerexception says: "for operation connection masterdatabase required. connection can't made width 'master'-database because original connection opened , references has been removed connection. please provide non-open connection"

after these action database inaccessible, deleted..

what can possibly done this? it's can't access master database because hostingprovider won't give me proper access right ofcourse.

since no other solution came decided change approach.

i've first created database myself , made sure correct sql user configured , had access.

then removed initializer , code global.asax file. after ran following command in package manager console (since layered design had select correct project in console);

enable-migrations 

after migrations enabled , made last minute changes entities ran command below scaffold new migration;

add-migration addsortorder 

after migrations created ran following command in console , voila, database updated entities;

update-database -verbose 

to able seed database when running migration i've overridden seed method in configuraton.cs class, created when enabling migrations. final code in method this;

protected override void seed(mycontext context)         {             //  method called after migrating latest version.              //add menu items , pages             if (!context.menu.any() && !context.page.any())             {                 context.menu.addorupdate(new menu()                                              {                                                  id = guid.newguid(),                                                  name = "mainmenu",                                                  description = "some menu",                                                  isdeleted = false,                                                  ispublished = true,                                                  publishstart = datetime.now,                                                  lastmodified = datetime.now,                                                  publishend = null,                                                  menuitems = new list<menuitem>()                                                                  {                                                                      new menuitem()                                                                          {                                                                              id = guid.newguid(),                                                                              isdeleted = false,                                                                              ispublished = true,                                                                              publishstart = datetime.now,                                                                              lastmodified = datetime.now,                                                                              publishend = null,                                                                              name = "some menuitem",                                                                              page = new page()                                                                                         {                                                                                             id = guid.newguid(),                                                                                             actionname = "some action",                                                                                             controllername = "somecontroller",                                                                                             ispublished = true,                                                                                             isdeleted = false,                                                                                             publishstart = datetime.now,                                                                                             lastmodified = datetime.now,                                                                                             publishend = null,                                                                                             title = "some page"                                                                                         }                                                                          },                                                                      new menuitem()                                                                          {                                                                              id = guid.newguid(),                                                                              isdeleted = false,                                                                              ispublished = true,                                                                              publishstart = datetime.now,                                                                              lastmodified = datetime.now,                                                                              publishend = null,                                                                              name = "some menuitem",                                                                              page = new page()                                                                                         {                                                                                             id = guid.newguid(),                                                                                             actionname = "some action",                                                                                             controllername = "somecontroller",                                                                                             ispublished = true,                                                                                             isdeleted = false,                                                                                             publishstart = datetime.now,                                                                                             lastmodified = datetime.now,                                                                                             publishend = null,                                                                                             title = "some page"                                                                                         }                                                                          }                                                                  }                                              });             }              if (!context.componenttype.any())             {                 context.componenttype.addorupdate(new componenttype()                 {                     id = guid.newguid(),                     isdeleted = false,                     ispublished = true,                     lastmodified = datetime.now,                     name = "mycomponent",                     publishend = null,                     publishstart = datetime.now                 });             }               try             {                 // code...                 // before try if know exception occurs in savechanges                  context.savechanges();             }             catch (dbentityvalidationexception e)             {                 //foreach (var eve in e.entityvalidationerrors)                 //{                 //    console.writeline("entity of type \"{0}\" in state \"{1}\" has following validation errors:",                 //        eve.entry.entity.gettype().name, eve.entry.state);                 //    foreach (var ve in eve.validationerrors)                 //    {                 //        console.writeline("- property: \"{0}\", error: \"{1}\"",                 //            ve.propertyname, ve.errormessage);                 //    }                 //}                 //throw;                  var outputlines = new list<string>();                 foreach (var eve in e.entityvalidationerrors)                 {                     outputlines.add(string.format(                         "{0}: entity of type \"{1}\" in state \"{2}\" has following validation errors:",                         datetime.now, eve.entry.entity.gettype().name, eve.entry.state));                     foreach (var ve in eve.validationerrors)                     {                         outputlines.add(string.format(                             "- property: \"{0}\", error: \"{1}\"",                             ve.propertyname, ve.errormessage));                     }                 }                 system.io.file.appendalllines(@"c:\temp\errors.txt", outputlines);                 throw;             }         } 

the disadvantage @ moment have manually migrate (only) 2 commands in package manager console. same time, fact doesn't happen dynamically because prevents possibly inwanted changes database. further works perfect.


Comments