Dan*_*zes 6 .net entity-framework .net-core
我正在按照教程制作 dotnet 2 rest api,并期望 dotnet cli 在 dotnet ef 更新后生成数据库。但它没有发生
我有这个上下文类
public class DutchContext : DbContext
{
public DutchContext(DbContextOptions<DutchContext> options): base(options)
{
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我在 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"));
});
}
Run Code Online (Sandbox Code Playgroud)
以及保存 con 字符串的 config.json
{
"ConnectionStrings": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=EstudoDotnetDB;Integrated Security=true;MultipleActiveResultSets=true"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 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.
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
对不起,如果这是一个愚蠢的问题,我是 dotnet 2 的新手。
我会尝试删除迁移文件夹并重新开始。我以前遇到过这个问题,不得不删除 ef 迁移并重新开始。
1)手动删除您的数据库- 无论它在哪里(我假设您已对连接进行排序),或者清空它,但更容易/更安全的是将它一起删除 - 因为有系统 __MigrationHistory 表 - 你也需要删除它。
2)删除所有迁移文件- 在迁移下 - 并命名为数字等 - 将它们全部删除,
3)重新构建包含迁移(和其余部分)的项目 - 并确保您的项目设置(配置)以自动构建(有时可能会导致问题 - 但对您来说不太可能),
4)再次运行 Add-Migration Initial - 然后 Update-Database
| 归档时间: |
|
| 查看次数: |
6965 次 |
| 最近记录: |