.net 代码--首先,当我执行enable-migrations时,我得到“No context type was found in the assembly 'DOC'”

bn31dyow  于 2023-04-08  发布在  .NET
关注(0)|答案(4)|浏览(114)

我使用的是Code-First,使用标准模板创建一个网站,然后不断向主applicationDBContext添加表,但是现在当我在Package ManagerConsole中执行Enable-Migrations时,我会得到
在程序集“DOC”中找不到上下文类型。
当我把完整的路径使用:

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

...我得到:
已在项目“DOC”中启用迁移。若要覆盖现有迁移配置,请使用-Force参数。
当我按下“强制”键时,它说
为项目DOC启用了代码优先迁移。
但是当我尝试添加迁移时,我得到
错误CS 0311类型“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

。。。导致项目建设失败
唯一一次我设法让这个工作通过降级的entityframework从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>
z9zf31ra

z9zf31ra1#

您混淆了enable-migrationsadd-migration的参数。
enable-migrations具有上下文类型名称。您指定的是配置名称,而不是上下文。

enable-migrations -ContextTypeName DOC.Models.ApplicationDbContext

然后补充:

add-migration MyMigrationName -ConfigurationTypeName DOC.Migrations.Configuration

我不确定你是否需要这些参数,因为看起来你只有一个项目。
从IdentityDbContext继承并不重要。此外,EF没有版本6.3和6.4,它是6.1.3或EF Core。

q3aa0525

q3aa05252#

如果您的解决方案中有多个项目,并且您正在使用*代码优先方法,那么您是否检查您是否在包管理器控制台中选择了正确的“ 默认项目 *”。
请参考下面的图片
试试看

vlju58qv

vlju58qv3#

尝试创建一个新项目,只是为了检查我们需要什么。好的,你去文件〉新建〉项目,在visual c#菜单下新建asp.netweb应用程序,为项目选择任何名称,这只是为了检查它在你的项目中缺少什么。你点击确定,在左边选择MVC,在右边是缺少的步骤。你可以看到一个更改身份验证按钮,在那个按钮下面写着,认证:没有身份验证,点击那里,选择个人用户帐户。点击确定和该选项,自动安装实体和身份。这就是问题。希望我能帮助,随时再次与我联系。

3phpmpom

3phpmpom4#

如果你使用EntityFrameworkCore和多个DBContext,这为我解决了这个问题:

PM>EntityFrameworkCore\Add-Migration AName -Context my.namespace.ADbContext

相关问题