如何在类库项目中配置实体框架 [英] How Do i configure Entity framework in class Library project

查看:58
本文介绍了如何在类库项目中配置实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含实体框架对象的类库项目,该对象将负责我的多个项目的公共数据访问层.将生成的dll文件添加到我的域项目中并使用类库项目中的实体对象后,我正面临以下问题.

在应用程序配置文件中找不到名为"ABC"的连接字符串.

我已将edmx的元数据工件处理属性设置为嵌入到输出程序集中

App.Config标记

  <?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>

实体代码如下

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

我要从数据目录"App_Data"访问数据库的另一件事

我正在引用dotnetcurry.com上的博客.

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

提前谢谢!

解决方案

基于链接您给了,可能您错过了这一部分.

现在您可以在该项目中创建自己的.config文件 并添加模型的连接字符串条目,或者直接复制 您在MyEntityModel项目中创建的App.config文件中, 您的控制台应用程序项目.我们将采用更简单的方法.右键点击 项目> 添加现有项>转到MyEntityModel项目,然后 添加App.config文件.

说明:

当您创建一个库并从另一个项目引用该库时,EF将读取与启动项目相关的配置.您可以执行以下操作之一.

  • 从库的配置中复制配置(复制文件并将其粘贴以启动项目)
  • 创建新配置,但复制连接字符串信息和其他与EF相关的部分
  • 通过添加现有项目来添加现有配置,并将其添加为库配置的链接

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!

解决方案

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

这篇关于如何在类库项目中配置实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆