Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF4 and Connection String

I have a 3 tiered project.

1) Project.Data (EDMX file)
2) Project.Model (POCO's)
3) Project.Console (Console app)

I have added the connection string into the Project.Console.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings>
        <add name="ProjectEntities" connectionString="metadata=res://*/Project.csdl|res://*/Project.ssdl|res://*/Project.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
</configuration>

The Project.Model is built using the EntityObject T4 Template in VS2010. It generates a ObjectContext class, with this constructor:

public ProjectEntities() : base("name=ProjectEntities", "ProjectEntities")
{
    this.ContextOptions.LazyLoadingEnabled = true;
    OnContextCreated();
}

I am just trying to instantiate the context object, in the Project.Console:

namespace Project.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            ProjectEntities pe = new ProjectEntities();
        }
    }
}

However, I am getting a MetadataException was unhandled error at the constructor. Stating Unable to load the specified metadata resource.

I have done a ton of research (Googling), and found that it seems to be a linking issue on those resources. I cannot seem to find a resolution.

Any help is appreciated.

like image 257
Dustin Laine Avatar asked Oct 21 '10 07:10

Dustin Laine


People also ask

What is connection string in Entity Framework?

A connection string contains initialization information that is passed as a parameter from a data provider to a data source. The syntax depends on the data provider, and the connection string is parsed during the attempt to open a connection.

What is your connection string?

In computing, a connection string is a string that specifies information about a data source and the means of connecting to it. It is passed in code to an underlying driver or provider in order to initiate the connection.

Where is connection string for Entity Framework?

ConnectionString Name You can also define the connection string in app. config or web. config and specify the connection string name starting with "name=" in the base constructor of the context class. Consider the following example where we pass the name=SchoolDBConnectionString parameter in the base constructor.

How do I change the connection string in Entity Framework?

If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.


2 Answers

I wrote a lengthy guide to debugging this error a while back.

like image 172
Craig Stuntz Avatar answered Sep 20 '22 12:09

Craig Stuntz


Open your assembly using any resource viewer (e.g., RedGate .NET Reflector) and check that the name of the metadata resource is the same that you have specified in the app.config.

like image 42
Devart Avatar answered Sep 23 '22 12:09

Devart