我建立下一个结构的WCF服务应用程序:如何使用EF6更改dbContext上的连接字符串?
WCF Client -> WCF Service -> Business Layer -> Repository -> EF6
我的应用程序需要该服务收听到多个客户端,每个至极的可以要求来自不同数据库(同一型号)的数据。因此,客户端使用Connect Method来告诉服务器需要使用哪个数据库。 为此,我使用创建连接字符串的类。我的问题是如何告诉上下文来改变它的连接字符串。到目前为止,我管理的覆盖这样的构造函数来解决此问题:
public partial class OhmioNETEntities : DbContext
{
public OhmioNETEntities() : base(SingleConnection.ConString)
{
}
}
这工作,但问题是,如果我刷新模式,这个码是
public partial class OhmioNETEntities : DbContext
{
public OhmioNETEntities() : base("name=OhmioNETEntities")
{
}
}
丢失和更换有这样做的最好方法? 谢谢!
编辑 感谢所有的帮助,但我不能得到它的工作。 作为建议我添加新的文件与此代码:
public partial class OhmioNETEntities : DbContext
{
public OhmioNETEntities(string ConString)
{
}
}
,当我创建上下文我使用:
context = new OhmioNETEntities(SingleConnection.ConString);
,并得到这个错误:
{"The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715 "}
任何想法,为什么?谢谢!
您可以编辑model.tt模板文件进行更改生成的代码,每次持续。 –