简体   繁体   English

WCF:使用实体框架-App.config

[英]WCF: Work with Entity Framework- App.config

I have three project in a solution, let call them client, serivce and host respectively. 我在解决方案中有三个项目,分别称为客户端,服务和主机。

I follow https://msdn.microsoft.com/en-us/library/ms734712%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 to build a service, which runs on a console host built by me, too. 我遵循https://msdn.microsoft.com/zh-cn/library/ms734712%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396构建服务,该服务在由我构建的控制台主机上运行也一样

I also use Entity Framework in service in order to store the data into MySql. 我还在服务中使用实体框架,以便将数据存储到MySql中。

Now the question: Entity Framework has something realated with app.config in service project. 现在的问题是:Entity Framework在服务项目中使用app.config实现了某些功能。 However, the link above from Microsoft tells me to run the host as the start project, leading to the fact that service cannot find it's config, which is in service project instead of host project. 但是,Microsoft上面的链接告诉我将主机作为启动项目运行,从而导致服务无法找到其配置的事实,该配置位于服务项目而非主机项目中。

If I replace app.config in project host with app.config in project service, it throws some exception. 如果我将项目宿主中的app.config替换为项目服务中的app.config,则会引发一些异常。

So what should I do to make my code work? 那么,应该怎么做才能使代码正常工作?

App.config: 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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
             requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- 部署服务库项目时,必须将配置文件的内容添加到
 主机的 app.config 文件中。System.Configuration 不支持库的配置文件。 -->
  <system.serviceModel>
    <services>
      <service name="Server.CalculatorService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/Server/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- 除非完全限定,否则地址相对于上面提供的基址-->
        <endpoint address="" binding="basicHttpBinding" contract="Server.ICalculator">
          <!-- 
              部署时,应删除或替换下列标识元素,以反映
             用来运行所部署服务的标识。删除之后,WCF 将
              自动推断相应标识。
          -->
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 -->
        <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
          以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <!--  <entityFramework>-->
  <!--    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">-->
  <!--      <parameters>-->
  <!--        <parameter value="mssqllocaldb" />-->
  <!--      </parameters>-->
  <!--    </defaultConnectionFactory>-->
  <!--    <providers>-->
  <!--      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
  <!--      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>-->
  <!--    </providers>-->
  <!--   -->
  <!--  </entityFramework>-->

  <entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient"
                type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="Model1Container" providerName="System.Data.SqlClient"
         connectionString="Server=127.0.0.1;user id=root;password=88;persistsecurityinfo=True;database=guessFigure" />
    <add name="RankADONETModel"
         connectionString="server=127.0.0.1;user id=root;password=88;persistsecurityinfo=True;database=guessFigure"
         providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
</configuration>

you need also config the section like this under the "configuration", change the sql provider to mysql 您还需要在“配置”下配置这样的部分,将sql provider更改为mysql

   <entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="mssqllocaldb" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM