web-dev-qa-db-ja.com

コードファースト、移行を有効にすると、「アセンブリ 'DOC'でコンテキストタイプが見つかりませんでした」と表示される

私はコードファーストを使用しており、標準テンプレートを使用してWebサイトを作成してから、メインのapplicationDBContextにテーブルを追加し続けましたが、今はEnable-MigrationsinPackage ManagerConsole I get

アセンブリ 'DOC'でコンテキストタイプが見つかりませんでした。

そして私が使用して完全なパスを置くとき:

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

...私は得る:

移行はプロジェクト「DOC」ですでに有効になっています。既存の移行構成を上書きするには、-Forceパラメーターを使用します。

そして、私が行くとき-Force、それは言う

プロジェクトDOCで有効化されたコードファーストマイグレーション。

add-migrationをしようとすると、

エラー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

...プロジェクトのビルドに失敗する

私が管理した唯一の時間は、entityframeworkを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>
7
Marwan Butrus

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です。

4
Steve Greene

ソリューション内に複数のプロジェクトがあり、Code Firstアプローチを使用している場合は、確認しましたかパッケージマネージャーコンソールで正しい「デフォルトプロジェクト」を選択したかどうか。

下の画像を参照してください

それを試してみてください。

enter image description here

2
Sunny

必要なものを確認するためだけに、新しいプロジェクトを作成してみてください。 Ok so go file> new> project、visual c#menu new asp.net web application、select any name for the project、this is not check it what what it in your project。You [OK]をクリックし、左側でMVCを選択します。右側には手順がありません。認証の変更ボタンが表示され、そのボタンの下に「認証:認証なし」と表示されているので、そこでクリックして個々のユーザーアカウントを選択します。[OK]をクリックしてそのオプションでは、エンティティとIDを自動的にインストールします。これが問題です。私がお手伝いできれば幸いです。またお気軽にご連絡ください。

0
Aravindhan R