c# - EF4 Code-First causes InvalidOperationException -


i'm having issue when trying run project each time builds. seems initializer runs, when comes first query - dies following invalidoperationexception.

this operation requires connection 'master' database. unable create connection 'master' database because original database connection has been opened , credentials have been removed connection string. supply unopened connection. 

for reference, i'm using ef code first ctp4, imported directly nuget. connecting sql server 2008 r2

what want happen re-create database if there model amendments , seed few values lookup table. both of these things seem supported* out of box.

my setup so:

global.asax

 protected void application_start()  {     database.setinitializer<coredb>(new coredbinitialiser());     // et al...  } 

coredb.cs

public class coredb : dbcontext {     public dbset<user> users { get; set; }     public dbset<login> logins { get; set; }     public dbset<permission> permissions { get; set; }     public dbset<role> roles { get; set; }     public dbset<rolepermission> rolepermissions { get; set; }     public dbset<userrole> userroles { get; set; }     public dbset<setting> settings { get; set; } }  public class coredbinitialiser : recreatedatabaseifmodelchanges<coredb> {     protected override void seed(coredb context)     {         var settings = new list<setting>         {             new setting             {                 settingname = "examplesetting",                 settingvalue = "this sample setting value",             }         };          settings.foreach(d => context.settings.add(d));     } } 

when runs, dies on line similar this, first query comes across after creating database.

user data = (from u in _data.users u.username == username  select u).singleordefault(); 

things don't think is:

  • it's not permissions: i've deleted actual database within sql server. application recreates around same time query attempted run (the initializer set, holds off creating until it's needed). i've logged on sql server user specified in web.config , have full read/write access database. in fact, should account creates databases also.
  • the database being created: deleting db , automatically recreates fine.
  • the connection string correctly defined, including providername attribute.

<add name="coredb" connectionstring="data source=localhost\sqlexpress;initial catalog=thedatabase;user id=theusername;password=thepassword;" providername="system.data.sqlclient" />

  • it doesn't appear bug in code/logic, once query has failed successfully, the code start until next time application rebuilt. it's be, , i'd have apply work around in code no matter anyway. :)

what do?

ideally, i'd "not think database schema" much. i'd seemed in scott gu's excellent blog post (and follow working existing databases) things worked out , disappeared away. part true. seems issue connection not being closed @ point, can't find out how rectify issue.

a few forum / posts imply issue i'm having because initializers aren't working planned , connection might left open. solution in other places appears to "don't create own initializer", isn't greatest solution - unless has ideas, i'll have until ctp5 maybe.

*yep, know it's ctp, "supported" not word :)

i know late answer, post high on goolge , answer may useful someone. problem missing credentials. prevent need change connection string have:

trusted_connection=false;persist security info=true 

based on this article


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -