4

I know this is a common issue faced by beginners in EF and there have been multiple questions and answers on the same here, but still I have not been able to still resolve the issue.

I am using EF 4.1 and MVC 3 and have generated the EF model from the DB in a separate library. I have copied the connection string from the app.config in the supporting library to the web.config of my application. I am also instantiating the object context by passing the web.config connection string.

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MasterDataContainer"].ConnectionString;

context = new MasterDataContainer(connectionString);

The connection string in web.config is as below

<connectionStrings>    
    <add name="MasterDataContainer" connectionString="metadata=res://*/MasterData.csdl|res://*/MasterData.ssdl|res://*/MasterData.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=MasterData;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

I am getting the error "Keyword not supported : data source". Any help on this is appreciated.

2 Answers 2

3

You don't need the EntityConnectionStringBuilder since you already have an EF connection string. i.e. just

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MasterDataContainer"].ConnectionString;
context = new MasterDataContainer(connectionString);

EntityConnectionStringBuilder can be used to build up an EF connection from e.g. a vanilla .NET one.

Edit It looks like you've encountered this problem here. The workaround is to escape the load the connection string and then replace the &quot; with "'"

What might be easier altogether is to use the name=ConnStringName overload of ObjectContext / DbContext. In your case, this would be

context = new MasterDataContainer("name=MasterDataContainer");

Note also if you view the source of the generated Context (probably MasterDataContainer.Context.cs) that the default constructor should have the EntityContainerName property hardcoded into it, so you might not need to provide a connectionstring at all to the constructor, provided that you keep the same EntityContainerName.

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks fot pointing that out. I have updated the code, but the primary issue still persists.
@Klaas I've done a bit more homework - hopefully one of these suggestions will help.
It turns out I had kept the providerName as SqlClient in web.config all along (I had copied the connection string from app.config thinking that I had pasted the exact same thing in web.config). Now I have corrected that and it works. My Bad!. Thanks a lot for those suggestions, especially the EntityContainerName. Didn't know that till now.
0

easiest way to solve this issue is rewrite your EF conn string (replacing the amp; and quot;):

from ORIGINAL

<add name="Entities" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=<SERVER\INSTANCE>;initial catalog=<BDD>;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;&quot;" providerName="System.Data.EntityClient" />

to FIXED

<add name="Entities" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string='metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient';provider connection string='data source=<SERVER\INSTANCE>;initial catalog=<BDD>;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework;'" providerName="System.Data.EntityClient" />

Just change then Entities name and the tags SERVER\INSTANCE and BDD with your values and that's it.

I hope this saves you a few days of research. Enjoy!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.