It seems that the Code First DbContext really uses the given ConnectionString during compile? I don’t even know how that is possible but to me it seems to be so. If I turn OFF my local SQL Server, I get the error stating “Failed to get the MetadataWorkspace for the DbContext type…”. Turning the SQL Server ON, everything compiles fine.
Here’s part of my context (I’m using an existing database and yes, I know, not actually code first)
public class MyContext : DbContext
{
public MyContext() : base("MY_DYNAMIC_CONNECTIONSTRING")
{
Database.SetInitializer<MyContext>(null);
}
...
If this is really the case, there’s a huge problem. How can I prevent it from doing that? What if I’m using separate build machines where the ConnectionString doesn’t work? Or am I doing something wrong? Any advice?
WCF RIA Services instantiates a
DbContextat design time and build time, not only at runtime:Quote from http://jeffhandley.com/archive/2011/06/30/RIAServicesCodeFirst.aspx:
Quote from http://varunpuranik.wordpress.com/2011/06/29/wcf-ria-services-support-for-ef-4-1-and-ef-code-first/#comment-102
If the connection string is not valid or the connection can’t be established you apparently get the exception you mentioned.