Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First DbContext Checks the ConnectionString During Compile?

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?

like image 985
Antti Simonen Avatar asked Sep 29 '11 13:09

Antti Simonen


People also ask

How do I test my EDMX connection string?

Open the edmx (go to properties, the connection string should be blank), close the edmx file again. Open the app. config and uncomment the connection string (save file) Open the edmx, go to properties, you should see the connection string uptated!!

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

What is DbContext class in Entity Framework?

A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns. Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance.


2 Answers

WCF RIA Services instantiates a DbContext at design time and build time, not only at runtime:

Quote from http://jeffhandley.com/archive/2011/06/30/RIAServicesCodeFirst.aspx:

In order to generate code into your Silverlight project, RIA Services has to inspect your DbContext at build time in order to get the entity types that are available.

Quote from http://varunpuranik.wordpress.com/2011/06/29/wcf-ria-services-support-for-ef-4-1-and-ef-code-first/#comment-102

The difference between EF CodeFirst stand alone and with RIA Services is that we initialize a new DbContext at design time as well.

If the connection string is not valid or the connection can't be established you apparently get the exception you mentioned.

like image 169
Slauma Avatar answered Sep 30 '22 13:09

Slauma


Here is the way that I use to track down the root cause of the "Failed to get the MetadataWorkspace for the DbContext type '{type}'" error:

http://joshmouch.wordpress.com/2011/11/09/failed-to-get-the-metadataworkspace-for-the-dbcontext-type-type/

I know it doesn't specifically answer your question, but it could help others who search Google for this error message.

like image 42
Josh Mouch Avatar answered Sep 30 '22 13:09

Josh Mouch