【问题标题】:Why do I have to build my Domain Project that contains my EDMX before my application will run?为什么我必须在我的应用程序运行之前构建包含我的 EDMX 的域项目?
【发布时间】:2023-04-05 09:20:02
【问题描述】:

我将 ASP.NET MVC 应用程序解决方案拆分为 3 个项目:

  • MyApp.Domain(EDMX 文件和存储库)
  • MyApp.Test(单元测试)
  • MyApp.WebUI(MVC 应用)

当我让我的应用程序脱离源代码控制,将项目MyApp.WebUI 设置为启动项目并按 F5 运行时,我的 DbContext 出现错误...

“System.InvalidOperationException”类型的异常发生在 mscorlib.dll 但未在用户代码中处理

附加信息:实体框架提供程序类型 'MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6' 在 ADO.NET 提供程序的应用程序配置文件中注册 无法加载具有不变名称“MySql.Data.MySqlClient”。制作 确保使用了程序集限定名称并且程序集是 可用于正在运行的应用程序。看 http://go.microsoft.com/fwlink/?LinkId=260882 了解更多信息。

但是,如果我先在我的MyApp.Domain 项目上单击鼠标右键并“构建”然后运行它,我的应用程序就可以很好地建立连接。

难道我不应该只单击运行,它就应该将它们全部构建并正常工作吗?

更新 1

如果我从构建菜单中选择“全部重建”或“构建解决方案”,我认为这应该单独重建我的所有项目,但在我运行它时会出现错误。

我必须在 VS 中对我的 MyApp.Domain 项目实际选择“构建”,然后按 F5 我的解决方案,以便我的应用程序在没有 DbContext 错误的情况下连接到数据库。

更新 2

如果我用鼠标右键单击我的 EDMX 文件并“运行自定义工具”(如 Microsoft article to regenerate the EDMX code 中所建议的那样),然后按 F5 键运行我的解决方案,它仍然无法运行。我也试过打开 EDMX 并保存它。它仍然会出现 DbContext 错误。

更新 3 - 应用配置文件

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<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>
  <connectionStrings>
    <add name="QuizEntities" connectionString="metadata=res://*/QuizModel.csdl|res://*/QuizModel.ssdl|res://*/QuizModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;database=uquiznew&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="owin:AppStartup" value="uQuiz.OwinStart" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

【问题讨论】:

  • 可能Domain 项目似乎已过时,因为它是一个依赖项,Visual Studio 需要“刷新”。
  • 我的解决方案脱离了源代码控制,我想知道这是否会影响它..?当你说它已经过时了,你的意思是,当我按 F5 时它不应该构建我所有的项目吗?
  • 可能是edmx 构建添加了一个生成步骤,否则会丢失。
  • 你是唯一一个参与这个项目的人吗?项目可以由其他开发人员修改吗?我不是这方面的专家,我只是在这里猜测,但我认为Run 充其量只是进行增量构建。
  • 是的,我是唯一一个工作的人,我在我自己的个人 SVN 存储库中拥有它:)。

标签: .net asp.net-mvc entity-framework dbcontext edmx


【解决方案1】:

正如 Tim.Tang 在their answer here 中所说:

在EF6中,需要使用mysql connector 6.8.x,并添加 DbConfigurationTypeAttribute 给你 DbContext:

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class DemoContext : DbContext{}

MySqlEFConfiguration 在 6.8.x 中的 MySql.Data.Entity.EF6.dll 中。

AppConfig:-

<?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>
  <connectionStrings>
    <add name="QuizEntities" connectionString="metadata=res://*/QuizModel.csdl|res://*/QuizModel.ssdl|res://*/QuizModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;database=uquiznew&quot;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
     <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, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
     </providers>
  </entityFramework>
</configuration>

【讨论】:

  • 我不敢相信我错过了这个。谢谢你来救我!你无意中整理了我的这个问题stackoverflow.com/questions/25967745/…我需要测试它是否会自动构建...
  • 遗憾的是它不能自动构建,我仍然需要重新构建我的MyApp.Domain 项目才能让它工作,但是有了注释我现在可以更新我的数据库了!耶!
  • 如果你复制别人的话,你必须提供正确的署名,就像我在上面所做的那样。
猜你喜欢
  • 2022-07-16
  • 2020-01-14
  • 2020-11-10
  • 2014-03-11
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
相关资源
最近更新 更多