我有一个包含12个类库的项目。
在我的第一个库中,我有以下代码:
Program.cs
public class Program
{
public static async Task Main(string[] args)
{
await CreateHostBuilder(args).Build().RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.AddUserSecrets<Program>();
config.AddEnvironmentVariables();
config.AddKeyVaultConfiguration();
})
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<ImportWorker>();
var localConnectionString = @"Server=(localdb)\mssqllocaldb;Database=dev-db;Integrated Security=True;";
services.AddPersistence(localConnectionString);
});
}
}
这是我的代码,其中包含了持久性配置
public static IServiceCollection AddPersistence(this IServiceCollection services, string connectionString)
{
Asserter.NotNullOrEmpty(connectionString, nameof(connectionString));
services.AddTransient<IDataImportContextFactory, DataImportContextFactory>();
services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
services.AddDbContext<IDataImportContext, DataImportContext>(
options => options.UseSqlServer(connectionString),
ServiceLifetime.Transient,
ServiceLifetime.Transient);
return services;
}
这是我的上下文文件:
public class DataImportContext : DbContext, IDataImportContext
{
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Handled by EF Core.
public DataImportContext(DbContextOptions<DataImportContext> options) : base(options) { }
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Handled by EF Core.
public virtual DbSet<Job> Jobs { get; set; }
}
当我尝试添加迁移(添加迁移dbInit)时,出现以下错误消息:
无法创建类型为“ DataImportContext”的对象。为了 设计时支持的不同模式,请参见
我选择了包含上下文文件的类库作为Default项目。我还将类库设置为启动项目。但是我仍然会收到错误消息。
我在做什么错了?