我首先使用Entity Framework模型。完成我的网站后,我做了Publish
。我正在使用Entity Framework和使用来自settings.config的连接字符串进行数据库连接设置:
<add key="thenna"
value="server=11.3.34.45;database=montage;user id=sample;password=Test;trusted_connection=false;"/>
我有配置更改了服务器数据库的详细信息。
web.config
中的实体框架连接字符串:
<add name="tickandtieEntities"
connectionString="metadata=res://*/Entityframework.Tickmarks.csdl|res://*/Entityframework.Tickmarks.ssdl|res://*/Entityframework.Tickmarks.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-QD6A981\SQLEXPRESS;initial catalog=tickandtie;user id=sa;password=tickmarks;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
当我使用服务器详细信息更改web.config
文件时出现错误
无法打开登录
请求的数据库“tickandtie”
当我将应用程序移动到主机服务器时,如何在web.config
中配置实体框架?请帮助我任何人
答案 0 :(得分:1)
您可以在创建时在EF Db Context上通过setting the connection string执行此操作,并将设置值传递给EF上下文。
例如:在你的上下文中添加一个构造函数,它使用基本的DbContext构造函数来传递连接字符串:
public class MyDbContext : DbContext
{
public MyDbContext(string connString) : base(connString)
{
}
}
然后使用上下文:
var connectionString = "" // Get the value of of your custom config file here.
var ctx = new MyDbContext(connectionString);
如上所述,您需要从settings.config
文件中读取连接字符串值 first 。