Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架模型第一个连接字符串_C#_Entity Framework - Fatal编程技术网

C# 实体框架模型第一个连接字符串

C# 实体框架模型第一个连接字符串,c#,entity-framework,C#,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文件时,我得到一个错误 无法打开登录请求的数据库“

我首先使用实体框架模型。在完成我的站点后,我发布了一个
。我正在使用实体框架和数据库连接设置,使用settings.config中的连接字符串:

<add key="thenna" 
     value="server=11.3.34.45;database=montage;user id=sample;password=Test;trusted_connection=false;"/>
当我用服务器详细信息更改
web.config
文件时,我得到一个错误

无法打开登录请求的数据库“tickandtie”

当我将应用程序移动到主机服务器时,如何在
web.config
中配置实体框架?请帮助我任何人

您可以在创建时在EF Db上下文上执行此操作,并将设置值传递给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
文件中读取连接字符串值

请详细解释itthank。我有Sql db连接字符串appsettings文件夹和实体框架连接字符串web配置文件,我在其中应用此代码?使用上述代码意味着您不需要在web.config中使用默认EF连接字符串。然后,您可以使用从特定位置(例如appSettings)获取。班
var connectionString = "" // Get the value of of your custom config file here.
var ctx = new MyDbContext(connectionString);