asp.net-mvc EntityFramework - 连接字符串在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5790325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:09:30  来源:igfitidea点击:

EntityFramework - Where is the connection string?

asp.net-mvcentity-framework-4ef-code-first

提问by Ian Warburton

I've deleted the 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.

我已经从我的 web.config 中删除了连接字符串,实体框架仍在连接到数据库!连接字符串在哪里设置?这是一个问题,因为我需要使网站的实时版本指向实时数据库。

回答by Omri Gazitt

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.

如果您的 DbContext 类(例如 Northwind)位于命名空间(例如 MvcProject)中,则出于某种原因,EF 不会将该类的名称与 web.config 中名为“Northwind”(或“MvcProject.Northwind”)的连接字符串相匹配,然后它只会创建一个默认为本地 SQLEXPRESS 实例的连接字符串,以及一个名为“MvcProject.Northwind”的数据库。这将是一个空数据库。并且您会试图弄清楚为什么没有取回数据,直到您意识到您没有连接到正确的数据库时,您会头疼不已。

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.

我解决这个问题的方法(不优雅,但它是我发现修复它的最快方法):向 DbContext 类添加一个构造函数,该类调用带有 web.config 中连接字符串名称的基类 - 例如

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

Hope that helps someone out there ;-)

希望能帮助那里的人;-)

回答by waz

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_Datafolder:

或者,如果您的数据库驻留在App_Data文件夹中:

<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 MyContextwith name of your class that extends DbContext.

替换MyContext为扩展DbContext.

回答by Beto Nogueira

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

EF 找不到我的连接,但我使用了 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.

只是为了测试连接,它的工作。

回答by Nathan Craddock

Convention > Configuration, right?

约定 > 配置,对吧?

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

默认情况下,EF Code fist 将在您的本地 SQL express 实例中创建一个数据库。

回答by Mubashar Sajjad

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.

我遇到了同样的问题,只是按照 web.config 文件中的建议将我的连接字符串名称更改为上下文数据库的名称,一切顺利。

回答by Chad Yeates

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

在 App.Config 中查看。它也会将它存储在那里。

回答by Alexander Taran

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.

如果您在 DbContext 中使用 codefirst 方法,您可以在 web.config 中放置一个名称与您的上下文类名称匹配的连接字符串,它会正常工作。

回答by neebz

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

右键单击您的实体框架模式(edmx 文件)> 转到属性。您将在那里看到连接字符串。

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

如果您的实体模型在一个单独的项目中,那么它必须在它自己的设置文件中。