开发者

EntityFramework - Where is the connection string?

I've deleted 开发者_如何学Gothe connection string from my web.config and Entity Framework is still connecting to the database! Where is the connection string being set? This is an issue because I need to make the live version of my website point to the live database.


Here's a gotcha I found with the "convention over configuration" philosophy when it comes to trying to connect to existing databases (like you're doing).

If your DbContext class (e.g. Northwind) is in a namespace (e.g. MvcProject), for some reason EF won't match the name of the class with a connection string in web.config named "Northwind" (or "MvcProject.Northwind"), and then it will just create a connection string defaulting to the local SQLEXPRESS instance, with a database called "MvcProject.Northwind". This will be an empty database. And you'll break your head trying to figure out why you're getting no data back, until you realize that you're not connected to the right DB.

The way I got around this (not elegant but it's the quickest way I found to fix it): add a constructor to your DbContext class that calls the base with the name of the connection string in web.config - e.g.

namespace MvcProject
{
    public class Northwind : DbContext
    {
        public Northwind() : base("Northwind") {}
    }
}

Hope that helps someone out there ;-)


You'll need something like this:

<configuration>
  <connectionStrings>
    <add name="MyContext"
         connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=YourDatabaseName"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Or if your database resides is App_Data folder:

<configuration>
  <connectionStrings>
    <add name="MyContext"
         connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|YourDatabaseFilename.mdf;User Instance=True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Replace MyContext with name of your class that extends DbContext.


The EF couldn´t find the connection for me but I used the connection string in the base():

namespace MvcProject 
{     
    public class Northwind : DbContext     
    {         
        public Northwind() : 
            base("Data Source=servername;Initial Catalog=database;User ID=yourID;Password=yourPass;Trusted_Connection=False;") {}
    }
}  

Just to test the connection and it´s worked.


Convention > Configuration, right?

By default, EF Code fist will create a database in your local SQL express instance.


Look in App.Config. It will store it there too.


I faced the same problem and simply changed my connection string name as suggested in web.config file as the name of context db and all went well.


Right click on your Entity Framework mode (edmx file) > goto Properties. You'll see the connection string there.

If your Entity Model is in a separate project, then it must be in it's own settings file.


If You are using codefirst aproach with DbContext you can place a connection string with name matching your context class name in your web.config and it will work just fine.


Connection strings with Database First Approach using EntityFrameWork 5.0 This is how it looks..

<add name="DbEntities" ConnectionString="metadata=res:///Models.DbEntities.csdl|res:/// Models.DbEntities.ssdl|res://*/Models.DbEntities.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="TNS_ADMIN =( Here we write the location where tns file is located) --example of tns file location is D:\app\client\oracle\product\12.2.0\client_2\network\admin\sample;
USER ID='';PASSWORD='';DATASOURCE='';PERSIST SECURITY INFO= True"" providerName="System.Data.EntityClient"/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜