Entity Connection String Constructor not found in EF 5 and WCF 4.5 at runtime

ぐ巨炮叔叔 提交于 2019-12-08 07:34:19

问题


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.

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>

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());

    }

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;
        }
    }

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 msg like:

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

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

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

Thanks in advance.

EDIT:

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;

        }

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'.

Please help me


回答1:


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)
    {
    }
}

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



来源:https://stackoverflow.com/questions/17462319/entity-connection-string-constructor-not-found-in-ef-5-and-wcf-4-5-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!