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.

4

2 回答 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

于 2012-08-24T06:18:32.847 回答
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!

于 2014-12-10T05:00:54.390 回答