Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Entity framework in class Library project?

I have created a class library project, which contains 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 the class library project, I am 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.

like image 979
Nik's Avatar asked Aug 20 '14 13:08

Nik's


People also ask

How do I add Entity Framework to an existing project?

Install Entity Framework to your Project. Right click on your project name and select Manage NuGet Packages. Go to Browse and Select Entity Framework then click Install button to install Entity Framework on your project.

How do I add EDMX file to class library project?

edmx . Go to solution Explorer > select Satyadatabasemodel.Context.tt under Satyadatabasemodel. edmx > Right click and Go to Properties > Enter "Custom Tool Namespace" value. Our class library project name is "Entities" > Save.


1 Answers

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
like image 176
Yuliam Chandra Avatar answered Oct 08 '22 18:10

Yuliam Chandra