[英]Code First Entity Framework - change connection string
How do I change the connection string in a code first entity framework/MVC application?如何更改代码优先实体框架/MVC 应用程序中的连接字符串? I'm trying to transfer it to a live site, but it overlooks web config values and still references my local version of the database.我正在尝试将其传输到实时站点,但它忽略了 web 配置值,并且仍然引用了我的本地数据库版本。
Here is the connection string section of my web.config:这是我的 web.config 的连接字符串部分:
<add name="MembershipConnectionString" connectionString="Data Source=192.168.1.43;Initial Catalog=Website.Models.IntranetApplication;User Id=[UserName];Password=[Password];timeout=30" />
<add name="WebsiteConnectionString" connectionString="Data Source=192.168.1.43;Initial Catalog=Website.Models.IntranetApplication;User Id=[UserName];Password=[Password];timeout=30" />
<add name="Entities" connectionString="metadata=res://*/Models.IntranetModel.csdl|res://*/Models.IntranetModel.ssdl|res://*/Models.IntranetModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=192.168.1.43;Initial Catalog=Website.Models.IntranetApplication;User Id=[UserName];Password=[Password];MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
I'm not sure if the Entities string has any relevance, as I used Code First entity framework, and I think that only appeared when I tried to create an edmx file (although I ended up just deleting it).我不确定 Entities 字符串是否有任何相关性,因为我使用了 Code First 实体框架,并且我认为这仅在我尝试创建 edmx 文件时出现(尽管我最终只是删除了它)。 The Entities connection string has sat commented out so I don't think it's used.实体连接字符串已被注释掉,所以我认为它没有被使用。
I want entity framework to read the "WebsiteConnectionString", but it seems to want to use the local connection string, but I can't even see where that is set.我希望实体框架读取“WebsiteConnectionString”,但它似乎想使用本地连接字符串,但我什至看不到它的设置位置。 How do I change it?我该如何改变它?
The connection string or its name can be passed to constructor of DbContext
.连接字符串或其名称可以传递给DbContext
的构造函数。 If you are using default constructor it searches for the connection string with the same name as the name of your derived context class and if it doesn't find it, it uses this one:如果您使用的是默认构造函数,它会搜索与派生上下文 class 名称相同的连接字符串,如果找不到,它会使用这个:
Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True
With database name same like your context class.数据库名称与您的上下文 class 相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.