我正在尝试使用 ASP.NET MVC3 EF 和 CODEFIRST 以及 SQL Server 2008 后端来发布网站。在我的本地计算机上,我使用 sql express 数据库进行开发,但现在我正在推送,我想使用我的托管生产数据库。问题是,当我尝试运行该应用程序时,它仍在使用我的本地数据库连接字符串。我已从 web.config 文件中完全删除了旧的连接字符串,并使用 <clear />
创建新连接字符串之前的标记。我还清理了解决方案并重建,但不知何故它仍然连接到旧数据库。我错过了什么?
这是新的连接字符串:
<connectionStrings>
<clear />
<add name="CellularAutomataDBContext"
connectionString=" Server=XXX;
Database=CellularAutomata; User ID=XXX; Password=XXX; Trusted_Connection=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
更新
当我调试并查看 DBCONTEXT 对象时,这是其连接所显示的内容:
Data Source=.\\SQLEXPRESS;Initial Catalog=CellularAutomata.Models.D1K2N3CARuleDBContext;Integrated Security=True;MultipleActiveResultSets=True"
我不确定为什么会发生这种情况,因为我在任何地方都找不到它被设置为这样。另外,在配置下它说 LazyLoadingEnabled = true,我认为这可能是问题的一部分,也许它没有加载新的连接字符串。在哪里更改这些参数?
更新2
EFCodeFirst 使用默认连接字符串,我不知道如何让它接受我在 web.config 文件中指定的连接字符串。
最佳答案
因此,使用 EF CodeFirst 时,它使用一个默认连接字符串。如果您希望能够使用自定义连接字符串,则必须遵循一些参数准则。
name ="this must match the name of your database context class"
connectionString="Server=yourserverurl; Database=yourdatabasename; User ID=youruserid;
Password=yourpassword; Initial Catalog=the name of the database to use;
Trusted_Connection=False"
providerName="System.Data.SqlClient"
到目前为止,这对我有用。
关于ASP.NET EFCodeFirst 未使用正确的连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5118747/