Sneha K

Sneha K

  • 1.2k
  • 527
  • 195.9k

Is it possible to use Entity client connection string to sql

Feb 12 2017 10:15 AM

Hi i want to know that how to use entity client connection string to sql connection of store procedure. Now i am connecting one db as Edmx file using Ado.net. so that i can get some default connection string in that web.config file.Now i am creating one store procedure in db and connect that store procedure as edmx file.

Then i used that in one controller. Now i have to declare sql connection to execute store procedure like

SqlConnection conn = new SqlConnection("DataSource")

In this same database name only i am using. So instead of writing connection code for every time i can call my web.config connetion string name.So that while deploying my project in online that time i will change dbname in web.config file. so that i can use every where once i call this connection string ever where. Now what i want is how to use Entity client connection string to sql connection . Is it possible in mvc.

My web.config file connceton string

<Connectionstring> <add name="COSECEntities" connectionString="metadata=res://*/Models.dbCOSEC.csdl|res://*/Models.dbCOSEC.ssdl|res://*/Models.dbCOSEC.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ADMIN-PC;initial catalog=COSEC;user id=sa;password=sql;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

My Controller (Exception)

 public class UserMasterController : Controller { private COSECEntities db = new COSECEntities(); SqlConnection conn =new SqlConnection("Data Source=ADMIN-PC;Initial Catalog=COSEC;User ID=sa;Password=sql;"); }

Now if i change the db name in web.config file means also i have to change where ever the i wrote sql connection place too.But if i call that web config file connection string name means it is enough to change in one place right.

I tried to call that web.config file connection in my controller but i got error that is can't use entity client connection strin to sql connection.Code is below.

Exception Controller

 public class UserMasterController : Controller { private COSECEntities db = new COSECEntities(); string strcon = ConfigurationManager.ConnectionStrings["COSECEntities"].ConnectionString; SqlConnection con = new SqlConnection(strcon);       con.Open(); }

if i change the provider name from entity clent to sql client in web.config file means another operations stopped working.

i tried my level best to explain my issue. Any one understand my issue and help me to resolve this problem

Thanks..


Answers (2)