code首先实体框架 - 改变连接字符串 [英] Code First Entity Framework - change connection string

查看:139
本文介绍了code首先实体框架 - 改变连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何改变一个code第一个实体框架/ MVC应用程序的连接字符串?我试图把它转移到一个活的网站,但它忽视网络配置值,仍然引用我的数据库的本地版本。

How do I change the connection string in a code first entity framework/MVC application? 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.config的连接字符串部分:

Here is the connection string section of my 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=&quot;Data Source=192.168.1.43;Initial Catalog=Website.Models.IntranetApplication;User Id=[UserName];Password=[Password];MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

我不知道,如果实体字符串有任何相关性,为我所用code一是实体框架,我认为只有当我试图创建一个EDMX文件出现(虽然我最终只是将其删除) 。实体连接字符串已经坐在注释掉了,所以我不认为这是使用。

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). The Entities connection string has sat commented out so I don't think it's used.

我想实体框架阅读WebsiteConnectionString,但它似乎想用本地连接字符串,但我甚至不能看到所设置。我该如何改变呢?

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. How do I change it?

推荐答案

在连接字符串或它的名字可以被传递到的DbContext 的构造函数。如果使用的是默认的构造函数将搜索与相同的名称派生上下文类的名称的连接字符串,如果没有找到它,它用这一个:

The connection string or its name can be passed to constructor of 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:

Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True

使用数据库名称一样喜欢你的上下文类。

With database name same like your context class.

这篇关于code首先实体框架 - 改变连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆