【问题标题】:Code-First, when I go enable-migrations, I get “No context type was found in the assembly 'DOC'”代码优先,当我启用迁移时,我得到“在程序集 'DOC' 中找不到上下文类型”
【发布时间】:2017-03-13 00:02:23
【问题描述】:

我正在使用 Code-First,使用标准模板创建一个网站,然后继续向主 applicationDBContext 添加表格,但是现在当我在 Package 中 Enable-Migrations经理 我得到的控制台

在程序集“DOC”中找不到上下文类型。

当我使用完整路径时:

ContextTypeName Option enable-migrations -ContextTypeName DOC.Migrations.Configuration

...我明白了:

已在项目“DOC”中启用迁移。要覆盖现有的迁移配置,请使用 -Force 参数。

当我使用 -Force 时,它会显示

为项目 DOC 启用 Code First 迁移。

但是当我尝试添加迁移时,我得到了

错误 CS0311 类型“DOC.Migrations.Configuration”不能用作泛型类型或方法“DbMigrationsConfiguration”中的类型参数“TContext”。没有从“DOC.Migrations.Configuration”到“System.Data.Entity.DbContext”的隐式引用转换。 DOC xxxxxxx\DOC\Migrations\Configuration.cs.

当我检查 Configuration.cs 文件时,它从以下位置更改:

internal sealed class Configuration : DbMigrationsConfiguration 

...到:

internal sealed class Configuration : DbMigrationsConfiguration

...导致项目构建失败

我唯一一次通过将实体框架从 6.1.3 降级到 6.1.2 然后重新升级来完成这项工作。

有时,我必须升级和降级几次,关闭 VS 并重新打开它才能工作。

有解决办法吗?这很烦人。

我还注意到我的DbContext 类派生自IdentityDBContext,这样可以吗?我知道通常你从DbContext 派生它,但由于我使用向导启动项目,它自动创建了它,我只是将我的其他表添加到它。

我的 web.config

    <?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=301880
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="data source=localhost\SQLTEST;initial catalog=DOC;User Id=xxx;Password=xxxx;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />
    <!--<add name="DefaultConnection" connectionString="data source=zzzzz;initial catalog=DOC;User Id=zzzzzz;Password=zzzzz;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />-->
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5.2" />
      </system.Web>
  -->
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <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="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.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="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.AspNet.Identity.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework.MappingAPI" publicKeyToken="7ee2e825d201459e" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.1.0.9" newVersion="6.1.0.9" />
      </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.WebPages" 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="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

【问题讨论】:

    标签: c# .net entity-framework ef-code-first entity-framework-migrations


    【解决方案1】:

    您混淆了enable-migrationsadd-migration 的参数。

    enable-migrations 有一个上下文类型名称。您正在指定配置名称,而不是上下文。

    enable-migrations -ContextTypeName DOC.Models.ApplicationDbContext
    

    然后添加:

    add-migration MyMigrationName -ConfigurationTypeName DOC.Migrations.Configuration
    

    我不确定您是否需要这些参数,因为看起来您只有一个项目。这是command reference

    从 IdentityDbContext 继承并不重要。另外,EF 没有 6.3 和 6.4 版本,它是 6.1.3 或 EF Core。

    【讨论】:

    • 它再次不起作用,所以当我输入建议的行时,我得到
      迁移已经在项目'DOC'中启用。要覆盖现有的迁移配置,请使用 -Force 参数。
      当我使用-Force参数时,我得到
      类型'DOC.Migrations.Configuration'不继承自'System.Data.Entity.Migrations.DbMigrationsConfiguration'。迁移配置类型必须从 'System.Data.Entity.Migrations.DbMigrationsConfiguration'

      扩展,关于 EF,对不起,我的意思是 6.1.3
    • 是的,您似乎已经启用了迁移。如果需要,您可以reset them。一切都在一个项目中,对吗?你不应该需要任何参数。 enable-migrations -> add-migration myNewMigration -> update-database.
    • 是的,在一个项目中,尝试添加迁移测试,我得到“在程序集 'DOC' 中找不到迁移配置类型。(在 Visual Studio 中,您可以使用来自的 Enable-Migrations 命令包管理器控制台添加迁移配置)。”
    • 您是否在控制台中选择了正确的项目?如果你做了很多降级和升级,你可能想要删除 EF,确保 web.config 是干净的,清理你的解决方案,删除你的 .suo 文件,然后重新添加 EF,等等。
    • 如果我删除它并重新安装,我必须卸载其他相关包,我只是做了 update-package -reinstall EntityFramework 然后我降级到 6.1.2 现在它可以工作了,事情是,它会在我关闭 VS 后再次中断并且不得不再次摆弄它,我实际上只是这样做并且它显示了相同的愚蠢消息(未找到迁移配置类型......等等)
    【解决方案2】:

    如果您的解决方案中有多个项目并且您使用 Code First 方法,那么您是否检查过您是否选择了正确的“默认项目”在包管理器控制台中。

    请参考下图

    试试看。

    【讨论】:

    • 小事,但有时需要很长时间才能发现。包管理器控制台的默认项目在我的情况下是错误的!谢谢。
    • 乐于助人:)
    【解决方案3】:

    尝试创建一个新项目,只是为了检查我们需要什么。好的,所以你去文件>新建>项目,在visual c#菜单下新建asp.net web应用程序,为项目选择任何名称,这只是为了检查你的项目中缺少什么。你点击ok,左边选择MVC,右边是缺少的步骤。您可以看到更改身份验证按钮,在该按钮下方显示身份验证:无身份验证,单击此处并选择个人用户帐户。单击确定和该选项,自动安装实体和身份。那就是问题所在。希望能帮到您,欢迎再次联系我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-11
      • 2012-10-13
      • 2016-01-27
      • 2013-04-18
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多