运行时在EF 5和WCF 4.5中找不到实体连接字符串构造函数 [英] Entity Connection String Constructor not found in EF 5 and WCF 4.5 at runtime

查看:111
本文介绍了运行时在EF 5和WCF 4.5中找不到实体连接字符串构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用VS 2010,WCF 4.0和EF 4.1.1开发了Web应用程序。我的WCF服务使用在web.config文件中配置的多个EF连接字符串开发,这些字符串取自实体模型app.config文件。基于参数,我在运行时使用EF连接字符串重定向数据库。

I have developed web application using VS 2010, WCF 4.0 and EF 4.1.1. My WCF Service developed with multiple EF Connection strings configured in web.config file those are taken from Entity Model app.config file. based on the parameters i am redirecting databases with EF Connection string at runtime.

我的WCF4.0 web.config如:

My WCF4.0 web.config like:

  <connectionStrings>
     <add name="WMSChennaiDEVEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=192.168.0.89;Initial Catalog=WMSCMSWPROD;User ID=sa;Password=wms@123;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
     <add name="WMSHyderabadDEVEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=192.168.0.89;Initial Catalog=WMSHMSWPROD;User ID=sa;Password=wms@123;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>

在我的WCF Service.svc.cs文件中,我编写了具有CenterId参数并基于这个ID我正在更改我的EF连接字符串,例如

And in my WCF Service.svc.cs file i have written function that has CenterId parameter and based on that ID i am changing my EF Connection strings like this

 WMSChennaiDEVEntities EcChennai;
    public void SetEntityModel(int CenterId)
    {
        if (CenterId == 4)
            EcChennai = new WMSChennaiDEVEntities(ConfigurationManager.ConnectionStrings["WMSChennaiDEVEntities"].ToString());
        else if (CenterId == 10)
            EcChennai = new WMSChennaiDEVEntities(ConfigurationManager.ConnectionStrings["WMSHyderabadDEVEntities"].ToString());

    }

现在我要将此Web应用程序转换为VS 2012我用相同的实体创建了新的EF模型,并创建了新的WCF服务。我做了一些更改,当我仅使用一个EF连接字符串时,一切工作正常。

Now I am going to convert this web application to VS 2012. I have created new EF Model with same entities and created new WCF Service. I have done some changes and everything is working fine when i am using only one EF Connection string.

WMSMainEntities EntCenter = new WMSMainEntities();
    public List<WMSCenter> GetCenters()
    {
        using (var tt = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
        {
            EntCenter.Configuration.LazyLoadingEnabled = false;
            EntCenter.Configuration.ProxyCreationEnabled = false;
            var CenterColl = EntCenter.WMSCenters.ToList();
            tt.Complete();
            tt.Dispose();
            return CenterColl;
        }
    }

现在,当我要去在运行时在函数中更改EF连接字符串。实体未占用连接字符串参数,因为显示消息不包含带有1个参数的构造函数。

Now I am in big trouble when i am going to change EF Connecction Strings at runtime in the function. Entity is not taking up Connection String parameter as showing message 'does not contain a constructor that takes 1 arguments'

WMSChennaiDEVEntities EcChennai;
public void SetEntityModel(int CenterId)
{
    if (CenterId == 4)
        EcChennai = new WMSChennaiDEVEntities(ConfigurationManager.ConnectionStrings["WMSChennaiDEVEntities"].ToString());
    else if (CenterId == 10)
        EcChennai = new WMSChennaiDEVEntities(ConfigurationManager.ConnectionStrings["WMSHyderabadDEVEntities"].ToString());
}

错误消息如:

Error   3   'CTScan.EntityLibrary.WMSChennaiDEVEntities' does not contain a constructor that takes 1 arguments  D:\Code\CTScan\CTScan.WCFService\CTScanService.svc.cs   66  29  CTScan.WCFService

我已经在该服务中开发了300多种方法。每个函数都取决于SetEntityModel(int CenterId)函数。

I have more than 300 methods already developed in the service. Every function is depend on the SetEntityModel(int CenterId) function.

因此,请帮助我解决如何在运行时问题中调用EF5连接字符串。

So please help me to resolve how to call EF5 connection strings in runtime issue.

请先谢谢。

编辑:

我尝试通过直接给连接字符串如下方式进行尝试:

I have tried by giving connection string directly as follows:

if (CenterId == 4)
        {
            EcChennai = new WMSHMSWPRODEntities();
            string ConStr = "metadata=res://*/CTScanCore.csdl|res://*/CTScanCore.ssdl|res://*/CTScanCore.msl;provider=System.Data.SqlClient;provider connection string='data source=192.168.0.89;initial catalog=WMSHMSWPROD;persist security info=True;user id=sa;password=wms@123;App=EntityFramework'";
            EcChennai.Database.Connection.ConnectionString = ConStr;

        }

更改:
1.删除了连接中的参数字符串'MultipleActiveResultSets = True;'
2.删除了& 并添加了单引号
3.错误消息不支持关键字:'metadata' 。在EcChennai.Database.Connection.ConnectionString = ConStr;

Changes: 1. Removed parameter in connection string 'MultipleActiveResultSets=True;' 2. Removed &quot; and added single quote 3. Error message Keyword not supported: 'metadata'. at EcChennai.Database.Connection.ConnectionString = ConStr;

Keyword not supported: 'metadata'.

请帮助我

推荐答案

在自动生成的实体类之外的部分类中添加要查找的构造函数:

Add the constructor you are looking for in a partial class outside of the auto-generated entity class:

public partial class WMSChennaiDEVEntities : DbContext
{
    public WMSChennaiDEVEntities(string connectionstring)
            : base(connectionstring)
    {
    }
}

此构造函数显然未包含在EF 5中,以防止当实体连接字符串为sql时我们意外地传递sql连接字符串。想要的。 不要错误地使用代码优先

This constructor is not included in EF 5 apparently to prevent us from accidentally passing a sql connection string when an entity connection string is desired. Don’t use Code First by mistake

这篇关于运行时在EF 5和WCF 4.5中找不到实体连接字符串构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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