我正在遵循制作dotnet 2 rest api的教程,并期望dotnet ef更新后dotnet cli能够生成数据库。但它没有发生
我有这个上下文类(class)
public class DutchContext : DbContext
{
public DutchContext(DbContextOptions<DutchContext> options): base(options)
{
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
}
}
这就是我在StartUp.cs上调用con字符串的方式
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IMailService, NullMailService>();
// support for real mail service
services.AddMvc();
services.AddDbContext<DutchContext>(cfg =>
{
cfg.UseSqlServer(_config.GetConnectionString("ConnectionString"));
});
}
还有保存con字符串的config.json
{
"ConnectionStrings": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=EstudoDotnetDB;Integrated Security=true;MultipleActiveResultSets=true"
}
}
这是我的localdb的名称>(localdb)\MSSQLLocalDB
和终端输出
$ dotnet ef database update
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using 'C:\Users\dbeze\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.0.1-rtm-125 initialized 'DutchContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
info: Microsoft.EntityFrameworkCore.Migrations[20405]
No migrations were applied. The database is already up to date.
No migrations were applied. The database is already up to date.
Done.
有任何想法吗?
抱歉,这是一个愚蠢的问题,我是dotnet 2的新手。
最佳答案
我会尝试删除迁移文件夹并重新开始。我之前遇到过这个问题,不得不删除ef迁移并重新开始。
1)手动删除数据库-不管它在哪里(我假设您的连接都已排序),或者将其清空,但是更容易/更安全的是将其全部删除-因为有系统__MigrationHistory表-您需要将其删除也。
2)删除所有迁移文件-在Migrations下-并命名为数字等。将它们全部删除,
3)重建包含迁移(和其余)的项目-并确保将您的项目设置(配置)为自动生成(有时可能会引起问题-但对您而言不太可能),
4)再次运行Add-Migration Initial -然后更新数据库
Post on how to delete and start over a EF migration
关于.net - Dotnet EF更新不生成数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47984857/