[英]Add migration to my class library ef core
I have a project with just one class library.我有一个只有一个类库的项目。 I'm now trying to generate a migration in the class library.
我现在正在尝试在类库中生成迁移。
Herer is my DbContext
:这是我的
DbContext
:
public class DatabaseContext : DbContext, IDatabaseContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{ }
public DbSet<Security> Services { get; set; }
public DatabaseFacade DatabaseAccessor => this.Database;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.EnableSensitiveDataLogging();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("dbo");
modelBuilder.Entity<Security>().HasNoKey();
base.OnModelCreating(modelBuilder);
}
}
When I run the command当我运行命令时
dotnet ef migrations add InitialCreate --project SecurityAttribute
I get the following error:我收到以下错误:
Unable to create an object of type 'DatabaseContext'.
无法创建类型为“DatabaseContext”的对象。 For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728
My IServiceCollections
looks like this:我的
IServiceCollections
如下所示:
public static IServiceCollection AddServiceAndRoleSecurity(this IServiceCollection services)
{
services.AddTransient<ISecurityService, SecurityService>();
services.AddMemoryCache();
services.AddTransient<ISecurityRepository, SecurityRepository>();
services.AddDbContext<IDatabaseContext, DatabaseContext>(options => options.UseSqlServer(
@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=SecurityAttribute;Integrated Security=SSPI;",
sqlServerOptions => sqlServerOptions.CommandTimeout(10 * 60)
));
services.AddTransient<IDatabaseContextFactory, DatabaseContextFactory>();
return services;
}
public static IHost CreateDatabase(this IHost host)
{
using (var scope = host.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<IDatabaseContext>();
var migrations = db.DatabaseAccessor.GetPendingMigrations();
if (migrations.Any())
db.DatabaseAccessor.Migrate();
}
return host;
}
My class library will be used as a nuget package.我的类库将用作 nuget 包。 That's why I only have a class library.
这就是为什么我只有一个类库。
I think the best solution is to create a test project with Startup.cs.我认为最好的解决方案是使用 Startup.cs 创建一个测试项目。 Where in configure services you register your
DbContext
and use this project for managing migrations.在配置服务的何处注册
DbContext
并使用此项目来管理迁移。
To specify where migrations should be added you can add this option to DbContext
options:要指定应在何处添加迁移,您可以将此选项添加到
DbContext
选项:
options.UseSqlServer(
connectionString,
x => x.MigrationsAssembly("MyLib.Migrations"));
where MyLib.Migrations
- name of the assembly where to store migrations where
MyLib.Migrations
- 存储迁移的程序集名称
Are you running your command (dotnet ef migrations add InitialCreate --project SecurityAttribute) in the package manager console?您是否在包管理器控制台中运行命令(dotnet ef migrations add InitialCreate --project SecurityAttribute)? If so do you have the default project set correctly in the package manager console drop down?
如果是这样,您是否在包管理器控制台下拉列表中正确设置了默认项目? Is the start up project for the solution set to be the one with the Startup.cs?
该解决方案的启动项目是否与 Startup.cs 相同?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.