'[DBContext の名前]' 型のオブジェクトを作成できません。デザイン時にサポートされるさまざまなパターンについては [closed] 質問する

'[DBContext の名前]' 型のオブジェクトを作成できません。デザイン時にサポートされるさまざまなパターンについては [closed] 質問する

私は、Udemy の ASP.NET MVC に関する Mosh Hamedani コースの 1 つを受講しています。

コードファースト (Entity Framework) を使用してデータベースを設計しているときに、1 つのエラーが発生しました。

最初は、「アセンブリ内に DbContext が見つかりません」この問題を解決した後、別の問題がどこからともなく発生しました。

下の画像は、移行の追加中に見つかったエラーを示しています。私はすでに同じエラーを検索しましたが、無駄でした。過去 2 時間苦労していますが、今のところ何も解決されていません。

誰か助けてください。ありがとう

'Vidly_Context' 型のオブジェクトを作成できません。設計時にサポートされるさまざまなパターンについては、https://go.microsoft.com/fwlink/?linkid=851728


(2)パラメータを持つ独自のDbContextコンストラクタを追加した後も同様の問題が発生しました。アプリは正常でしたが、移行が機能しなくなりました。最初にEFを更新することで修正しました(5で動作しているときに奇妙な理由で3.1.5を使用)。ドットネットツール @xspdf設定されていない場合は、前述のコンストラクターをメソッド + ハードコードされたデフォルトの接続文字列に置き換えます。

dotnet tool update --global dotnet-ef

// following command show the most during migration build/run in cmd
// mind current dir is Migrations folder of (VS) startup project here
dotnet ef --startup-project ../ --verbose migrations add test

3.1.5 & コンテキストアクティベーションエラー

The Entity Framework tools version '3.1.5' is older than that of the runtime '5.0.0'. Update the tools for the latest features and bug fixes.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly '...'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext '...Context'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type '...Context'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
 ---> System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate '...'. (my additional parameter)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13()
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create an object of type '...Context'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

ベストアンサー1

1- Webプロジェクトを次のように設定してくださいスタートアッププロジェクトとして設定

2- 中パッケージ マネージャー コンソールデータアクセス層(ある場合)を次のように設定します。デフォルトプロジェクト

3- コマンドを再度実行します

おすすめ記事