可实体框架的提供程序连接字符串在web.config中已经定义了一个数据库连接字符串替换? [英] Can the provider connection string of Entity Framework be substituted with a DB connection string already defined in web.config?

查看:171
本文介绍了可实体框架的提供程序连接字符串在web.config中已经定义了一个数据库连接字符串替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库连接字符串'ApplicationServicesweb.config中的ConnectionString部分,其中3具有相同的连接字符串作为一个在ApplicationServices供应商连接字符串属性的实体框架连接字符串定义。

I have a db connection string 'ApplicationServices' defined in the connectionString section of web.config and 3 Entity Framework connection strings which have the provider connection string attribute with the same connection string as the one in 'ApplicationServices'.

有没有参考的connectionString为在web.config中的EF连接字符串的提供程序连接字符串属性,而不是从头再来提供连接字符串'ApplicationServices的方法吗?这将减少错误,并有助于更轻松地部署应用程序。

Is there a way to reference connectionString in 'ApplicationServices' for the provider connection string attribute of the EF connection string in the web.config, rather than providing the connection string all over again? This will reduce errors and help deploy the application more easily.

推荐答案

生成的对象上下文有许多不同的方式来为它提供一个连接字符串。也正因为生成的类是部分你可以定义自己的构造函数,设置连接字符串。

The generated object context has many different ways to provide it with a connection string. Also because the generated class is a partial you can define your own constructor which sets the connection string.

http://msdn.microsoft.com/en-us/library/ bb156503.aspx

回复评论:

因此​​,从配置如下:

So read from the config:

public class MyContext : ObjectContext 
{
     public MyContext()
         : base(ConfigurationManager.ConnectionStrings["MyConnectionString"])
     { }
}

这篇关于可实体框架的提供程序连接字符串在web.config中已经定义了一个数据库连接字符串替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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