gpt4 book ai didi

c# - 代码优先,当我启用迁移时,我得到 “No context type was found in the assembly ' DOC'”

转载 作者:太空狗 更新时间:2023-10-29 23:05:31 24 4
gpt4 key购买 nike

我正在使用 Code-First,使用标准模板创建网站,然后不断向主页面添加表格 applicationDBContext ,但是现在当我在 Package Manager 控制台中进入 Enable-Migrations 时,我得到了

No context type was found in the assembly 'DOC'.

当我使用完整路径时:

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

...我明白了:

Migrations have already been enabled in project 'DOC'. To overwrite the existing migrations configuration, use the -Force parameter.

当我去 -Force 时,它说

Code First Migrations enabled for project DOC.

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

Error CS0311 The type 'DOC.Migrations.Configuration' cannot be used as type parameter 'TContext' in the generic type or method 'DbMigrationsConfiguration'. There is no implicit reference conversion from 'DOC.Migrations.Configuration' to 'System.Data.Entity.DbContext'. DOC xxxxxxx\DOC\Migrations\Configuration.cs.

当我检查 Configuration.cs 文件时,它更改为:

internal sealed class Configuration : DbMigrationsConfiguration 

...到:

internal sealed class Configuration : DbMigrationsConfiguration

...导致项目构建失败

我唯一一次通过将 Entity Framework 从 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>

最佳答案

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

关于c# - 代码优先,当我启用迁移时,我得到 “No context type was found in the assembly ' DOC'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42754713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com