EntityFramework - 连接字符串在哪里?
我已从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是我在尝试连接到现有数据库(就像您正在做的那样)时发现的“约定优于配置”哲学的问题。
如果您的 DbContext 类(例如 Northwind)位于命名空间(例如 MvcProject)中,则出于某种原因 EF 不会将该类的名称与 web.config 中名为“Northwind”(或“MvcProject.Northwind”)的连接字符串相匹配,然后它只会创建一个默认为本地 SQLEXPRESS 实例的连接字符串,以及一个名为“MvcProject.Northwind”的数据库。这将是一个空数据库。你会绞尽脑汁试图找出为什么没有返回数据,直到你意识到你没有连接到正确的数据库。
我解决这个问题的方法(不是很优雅,但这是我发现解决它的最快方法):向 DbContext 类添加一个构造函数,该构造函数使用 web.config 中的连接字符串名称调用基类 - 例如
希望能帮助别人那里 ;-)
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.
Hope that helps someone out there ;-)
您将需要这样的内容:
或者,如果您的数据库驻留在
App_Data
文件夹中:将
MyContext
替换为扩展DbContext
的类的名称。You'll need something like this:
Or if your database resides is
App_Data
folder:Replace
MyContext
with name of your class that extendsDbContext
.EF 找不到我的连接,但我在 base() 中使用了连接字符串:
只是为了测试连接,它有效。
The EF couldn´t find the connection for me but I used the connection string in the base():
Just to test the connection and it´s worked.
公约>配置对吧?
默认情况下,EF Code 会在本地 SQL Express 实例中创建一个数据库。
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.
我遇到了同样的问题,只是按照 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.
右键单击您的实体框架模式(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.
如果您在 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.
使用 EntityFrameWork 5.0 的数据库优先方法的连接字符串 这就是它的样子..
Connection strings with Database First Approach using EntityFrameWork 5.0 This is how it looks..