[英]Unable to create an object of type 'ApplicationDbContext'. Ef core 5.0
I using CleanArchitecture solution.我使用 CleanArchitecture 解决方案。 I have Data layer where ApplicationDbContext and UnitOfWork are located:
我有 ApplicationDbContext 和 UnitOfWork 所在的数据层:
namespace Portal.Data.MyDbContexts
{
internal class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(BaseEntity).Assembly);
}
// **********
public DbSet<WebsiteMonitoredCategory> WebsiteMonitoredCategories { get; set; }
// **********
}
unitofwork:工作单元:
public abstract class BaseUnitOfWork : object, IBaseUnitOfWork
{
//public UnitOfWork() : base()
//{
//}
public BaseUnitOfWork(Options options) : base()
{
Options = options;
}
// **********
protected Options Options { get; set; }
// **********
// **********
// **********
// **********
private ApplicationDbContext _databaseContext;
// **********
// **********
/// <summary>
/// Lazy Loading = Lazy Initialization
/// </summary>
internal ApplicationDbContext DatabaseContext
{
get
{
if (_databaseContext == null)
{
var optionsBuilder =
new DbContextOptionsBuilder<ApplicationDbContext>();
switch (Options.Provider)
{
case Provider.SqlServer:
{
optionsBuilder.UseSqlServer
(connectionString: Options.ConnectionString);
break;
}
case Provider.MySql:
{
//optionsBuilder.UseMySql
// (connectionString: Options.ConnectionString);
break;
}
case Provider.Oracle:
{
//optionsBuilder.UseOracle
// (connectionString: Options.ConnectionString);
break;
}
case Provider.PostgreSQL:
{
//optionsBuilder.UsePostgreSQL
// (connectionString: Options.ConnectionString);
break;
}
case Provider.InMemory:
{
optionsBuilder.UseInMemoryDatabase(databaseName: "Temp");
break;
}
default:
{
break;
}
}
_databaseContext =
new ApplicationDbContext(options: optionsBuilder.Options);
}
return _databaseContext;
}
}
and I have IoC Layer too for dependency injection to presentation layer:我也有 IoC 层,用于向表示层注入依赖项:
public class DependencyContainer
{
public static void RegisterServices(IServiceCollection services,IConfiguration configuration)
{
//DataLayer
services.AddTransient<IUnitOfWork, UnitOfWork>(_ =>
{
Options options =
new Options
{
Provider =
(Provider)System.Convert.ToInt32(configuration.GetSection(key: "DatabaseProvider").Value),
ConnectionString =
configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value,
};
return new UnitOfWork(options: options);
});
}
}
and finally I Have an Windows Forms application Net 5.0 (All Projects are Net5.0)最后我有一个 Windows Forms 应用程序 Net 5.0(所有项目都是 Net5.0)
namespace Portal.Desktop
{
public static class Program
{
private static IConfiguration Configuration { get; set; }
public static IServiceProvider ServiceProvider { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var services = ConfigureServices();
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
RegisterServices(services, Configuration);
}
public static void RegisterServices(IServiceCollection services, IConfiguration configuration)
{
DependencyContainer.RegisterServices(services, configuration);
}
public static IServiceCollection ConfigureServices()
{
var services = new ServiceCollection();
//services.AddTransient<IUnitOfWork, UnitOfWork>(_ =>
//{
// Options options =
// new Options
// {
// Provider =
// (Provider)System.Convert.ToInt32(Configuration.GetSection(key: "DatabaseProvider").Value),
// //using Microsoft.EntityFrameworkCore;
// //ConnectionString =
// // Configuration.GetConnectionString().GetSection(key: "MyConnectionString").Value,
// ConnectionString =
// Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value,
// };
// return new Portal.Data.UoW.UnitOfWork(options: options);
//});
ServiceProvider = services.BuildServiceProvider();
return services;
}
}
}
I usage Ef core 5.0, windows forms is set as startup project and package manager console set to Protal.Data (dbcontext and unitofwork in there) but when i run Add-Migration inti in PMC get me an error:我使用 Ef 核心 5.0、windows forms 设置为启动项目,package 管理器控制台设置为 Protal.Data,但在 PMC 中运行时得到一个错误( dbcontext和 unitofwork 运行时)
Unable to create an object of type 'ApplicationDbContext'.
无法创建“ApplicationDbContext”类型的 object。 For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728
installed this packages in data layer:在数据层安装了这个包:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
I don't know why, get me the error.不知道为什么,给我一个错误。
-----UPDATE---- - - -更新 - -
I follow the https://go.microsoft.com/fwlink/?linkid=851728 and added ApplicationDbContextFactory.cs to Portal.Desktop (windows forms Net5.0) project:我遵循https://go.microsoft.com/fwlink/?linkid=851728并将ApplicationDbContextFactory.cs添加到 Portal.Desktop (windows forms Net5.0) 项目:
namespace Portal.Desktop
{
public class ApplicationDbContextFactory:IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var connectionString = Program.Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value;
var provider = Program.Configuration.GetSection(key: "DatabaseProvider").Value;
var optionsBuilder =
new DbContextOptionsBuilder<ApplicationDbContext>();
var options =
new Options
{
Provider =
(Provider)System.Convert.ToInt32(provider),
//using Microsoft.EntityFrameworkCore;
//ConnectionString =
// Configuration.GetConnectionString().GetSection(key: "MyConnectionString").Value,
ConnectionString = connectionString,
};
switch (options.Provider)
{
case Provider.SqlServer:
{
optionsBuilder.UseSqlServer
(connectionString: options.ConnectionString);
break;
}
case Provider.MySql:
{
//optionsBuilder.UseMySql
// (connectionString: Options.ConnectionString);
break;
}
case Provider.Oracle:
{
//optionsBuilder.UseOracle
// (connectionString: Options.ConnectionString);
break;
}
case Provider.PostgreSQL:
{
//optionsBuilder.UsePostgreSQL
// (connectionString: Options.ConnectionString);
break;
}
case Provider.InMemory:
{
optionsBuilder.UseInMemoryDatabase(databaseName: "Temp");
break;
}
default:
{
break;
}
}
return
new ApplicationDbContext(options: optionsBuilder.Options);
}
}
}
When I want add a migration ef core throw Exception has been thrown by the target of an invocation.当我想添加迁移时,核心抛出异常已被调用的目标抛出。 exception:
例外:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.TargetInvocationException:调用的目标已引发异常。 ---> System.NullReferenceException: Object reference not set to an instance of an object.
---> System.NullReferenceException:Object 引用未设置为 object 的实例。 at Portal.Desktop.ApplicationDbContextFactory.CreateDbContext(String[] args) in C:\Users\Arman Es\source\repos\Spider\Portal.Desktop\ApplicationDbContextFactory.cs:line 20 --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
在 C 中的 Portal.Desktop.ApplicationDbContextFactory.CreateDbContext(String[] args):\Users\Arman Es\source\repos\Spider\Portal.Desktop\ApplicationDbContextFactory.cs:line 20 --- 内部异常堆栈跟踪结束 -- - at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture )
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory, Type contextType) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_2.b__9() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 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.b__0(在 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) 在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory, Type contextType) 在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_2 .b__9() 在 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.b__0( ) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Exception has been thrown by the target of an invocation.) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) 调用的目标已引发异常。
finally, I found my answers in this article https://snede.net/you-dont-need-a-idesigntimedbcontextfactory/最后,我在这篇文章https://snede.net/you-dont-need-a-idesigntimedbcontextfactory/中找到了我的答案
Create ApplicationDbContextFactory in Portal.Data project:在 Portal.Data 项目中创建ApplicationDbContextFactory :
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Portal.Domain.Tools;
using Portal.Domain.Tools.Enums;
namespace Portal.Data.MyDbContexts
{
public class ApplicationDbContextFactory:IDesignTimeDbContextFactory<ApplicationDbContext>
{
private IConfiguration Configuration { get; set; }
public ApplicationDbContext CreateDbContext(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
var optionsBuilder =
new DbContextOptionsBuilder<ApplicationDbContext>();
var options =
new Options
{
Provider =
(Provider)System.Convert.ToInt32(Configuration.GetSection(key: "DatabaseProvider").Value),
ConnectionString = Configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value
};
switch (options.Provider)
{
case Provider.SqlServer:
{
optionsBuilder.UseSqlServer
(connectionString: options.ConnectionString);
break;
}
case Provider.MySql:
{
//optionsBuilder.UseMySql
// (connectionString: Options.ConnectionString);
break;
}
case Provider.Oracle:
{
//optionsBuilder.UseOracle
// (connectionString: Options.ConnectionString);
break;
}
case Provider.PostgreSQL:
{
//optionsBuilder.UsePostgreSQL
// (connectionString: Options.ConnectionString);
break;
}
case Provider.InMemory:
{
optionsBuilder.UseInMemoryDatabase(databaseName: "Temp");
break;
}
default:
{
break;
}
}
return
new ApplicationDbContext(options: optionsBuilder.Options);
}
}
}
appsettings.json still in Portal.Desktop project: appsettings.json仍在 Portal.Desktop 项目中:
namespace Portal.Desktop
{
public static class Program
{
public static IConfiguration Configuration { get; set; }
public static IServiceProvider ServiceProvider { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var services = ConfigureServices();
RegisterServices(services, Configuration);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
public static void RegisterServices(IServiceCollection services, IConfiguration configuration)
{
DependencyContainer.RegisterServices(services, configuration);
}
public static IServiceCollection ConfigureServices()
{
var services = new ServiceCollection();
ServiceProvider = services.BuildServiceProvider();
return services;
}
}
}
and finally IoC project is:最后IoC项目是:
namespace Portal.IoC
{
public class DependencyContainer
{
public static void RegisterServices(IServiceCollection services,IConfiguration configuration)
{
//DataLayer
services.AddTransient<IUnitOfWork, UnitOfWork>(_ =>
{
var options =
new Options
{
Provider =
(Provider)System.Convert.ToInt32(configuration.GetSection(key: "DatabaseProvider").Value),
ConnectionString =
configuration.GetSection(key: "ConnectionStrings").GetSection(key: "MyConnectionString").Value,
};
return new UnitOfWork(options: options);
});
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.