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

时间:2017-03-13 00:02:24

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

我使用Code-First,使用标准模板创建网站,然后继续向主applicationDBContext添加表格,但现在当我去启用 - 迁移包管理器我得到的控制台

  

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

当我使用完整路径时:

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

......我明白了:

  

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

当我走 -Force 时,它说

  

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

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

  

错误CS0311类型' DOC.Migrations.Configuration'不能用作类型参数' TContext'通用类型或方法' DbMigrationsConfiguration'。来自' 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=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>

3 个答案:

答案 0 :(得分:2)

您会混淆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。

答案 1 :(得分:1)

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

尝试一下。

enter image description here

答案 2 :(得分:0)

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