EntityFramework - 连接字符串在哪里?

发布于 2024-11-03 06:50:25 字数 86 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

放我走吧 2024-11-10 06:50:25

这是我在尝试连接到现有数据库(就像您正在做的那样)时发现的“约定优于配置”哲学的问题。

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

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

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

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

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 ;-)

反话 2024-11-10 06:50:25

您将需要这样的内容:

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

或者,如果您的数据库驻留在 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>

MyContext 替换为扩展 DbContext 的类的名称。

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.

岁月流歌 2024-11-10 06:50:25

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;") {}
    }
}  

只是为了测试连接,它有效。

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.

简单爱 2024-11-10 06:50:25

公约>配置对吧?

默认情况下,EF Code 会在本地 SQL Express 实例中创建一个数据库。

Convention > Configuration, right?

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

淡水深流 2024-11-10 06:50:25

查看应用程序配置。它也会将其存储在那里。

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

燃情 2024-11-10 06:50:25

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

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.

自控 2024-11-10 06:50:25

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

如果您的实体模型位于单独的项目中,则它必须位于其自己的设置文件中。

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.

白鸥掠海 2024-11-10 06:50:25

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

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.

耀眼的星火 2024-11-10 06:50:25

使用 EntityFrameWork 5.0 的数据库优先方法的连接字符串 这就是它的样子..

<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"/

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"/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文