Code-First,当我进行启用迁移时,我得到“在程序集'DOC'中找不到上下文类型”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code-First,当我进行启用迁移时,我得到“在程序集'DOC'中找不到上下文类型”相关的知识,希望对你有一定的参考价值。

我正在使用Code-First,使用标准模板创建一个网站,然后继续向主applicationDBContext添加表,但是现在当我在Package Manager Console中启用Enable-Migrations时,我得到了

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

当我使用完整路径时:

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

......我明白了:

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

当我说 - Force时,它说

为项目DOC启用了代码优先迁移。

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

错误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并重新打开,然后它才能工作。

这个问题有方法解决吗?它非常烦人。

enter image description here

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

enter image description here

我的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=localhostSQLTEST;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>
答案

你混淆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。

另一答案

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

试试吧。

enter image description here

另一答案

尝试创建一个新项目,只是为了检查我们需要什么。好的,你去文件> new>项目,在visual c#menu新的asp.net web应用程序下,选择项目的任何名称,这只是检查项目中缺少的内容。单击“确定”,在左侧选择“MVC”,在右侧选择缺少的步骤。您可以看到更改身份验证按钮,并在该按钮下显示身份验证:无身份验证,单击此处并选择单个用户帐户。单击“确定”和该选项,自动安装实体和标识。那就是问题所在。希望我能提供帮助,请随时与我联系。

以上是关于Code-First,当我进行启用迁移时,我得到“在程序集'DOC'中找不到上下文类型”的主要内容,如果未能解决你的问题,请参考以下文章

Code-First 迁移问题

20.2.翻译系列:EF 6中基于代码的数据库迁移技术EF 6 Code-First系列

EFEF Code-First数据迁移

20.翻译系列:Code-First中的数据库迁移技术EF 6 Code-First系列

当还涉及迁移时,如何使用 django 进行同步数据库

译第36节---基于代码迁移