Nik's Nik's - 23 days ago 7
C# Question

How Do i configure Entity framework in class Library project

i have created a class library project which contain the entity framework object who will be responsible for common data access layer for my multiple project.after adding the generated dll file to my domain project and using the entity object from class library project, im facing with the following issue.

No connection string named 'ABC' could be found in the application config file.

i have set the Metadata Artifact processing property of edmx to Embed in Output Assembly

App.Config markup

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="ABC" connectionString="metadata=res://*/Entity.ABC.csdl|res://*/Entity.ABC.ssdl|res://*/Entity.ABC.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\ABC.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>


And Entities code are as follow

public ABC_IntegrationEntities()
: base("name=ABC")
{
}


one more thing i am accessing database from datadirectiory "App_Data"

i am referencing the blog from dotnetcurry.com.

http://www.dotnetcurry.com/showarticle.aspx?ID=617

Thanks in advance!

Answer

Based on the link you gave, probably you miss this part.

Now you can either create your own .config file in this project and add the connectionstring entry of your model or simply copy the App.config file that you created in the MyEntityModel project, in your Console App project. We will adopt the simpler way. Right click the project > Add Existing Item > Go to the MyEntityModel project and add the App.config file.

Explanation:

When you create a library and the library is referenced from another project, EF will read the config that is relative to start up project. You can do one of the following.

  • Copy the config from library's config (copy the file and paste it to start up project)
  • Create a new config but copy the connection string information and other EF related section
  • Add existing config by add existing item and add it as link to library's config