C# 检查是否使用添加迁移创建了数据库

C# 检查是否使用添加迁移创建了数据库,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,VisualStudio 2017社区,ASP.NET核心2。 我定义了一个dbContext: namespace MyDb.Models { public class MyDbContext : DbContext { public MyDbContext (DbContextOptions<MyDbContext> options) : base(options) { } public DbSet<Bl

VisualStudio 2017社区,ASP.NET核心2。 我定义了一个dbContext:

namespace MyDb.Models
{


    public class MyDbContext : DbContext
    {
        public MyDbContext (DbContextOptions<MyDbContext> options) : base(options)
        { }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
}
和startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
    services.AddLocalization(options => options.ResourcesPath = "Resources");
    services.AddOptions();
    services.Configure<MyOptions>(Configuration);
    services.AddMvc()
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddDataAnnotationsLocalization();
}
但现在,当我尝试在Razor页面中访问它时,即:

namespace MyDb
{
    public class ViewTablesModel : PageModel
    {
        private readonly MyDbContext _context;

        public ViewTablesModel(MyDbContext context)
        {
            _context = context;
        }

        public IList<Blog> Blog { get;set; }

        public async Task OnGetAsync()
        {
            Blog = await _context.Blogs.ToListAsync();
        }
    }
}
现在,错误略有不同:

Win32异常:未知错误(0x89c50107) 未知位置

SqlException:建立到SQL Server的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:SQL网络接口,错误:50-发生本地数据库运行时错误。Istanza del Database locale specificata Inistente

我正在从VS2017以调试模式启动应用程序。它应该关注SQL Server,不是吗? 无论如何,我看到一个“sqlservr.exe”进程正在执行


但是尝试手动连接到该数据库(使用.udl文件)仍然失败。

数据库不是通过添加迁移创建的。而是只创建带有
CreateTable(…)
etc语句的中间代码,如果需要,可以修改


要应用迁移,请使用
更新数据库
命令。它还将为初始迁移创建数据库。

验证sql server是一个问题。请将计算机上的空白文本文件重命名为.udl扩展名。然后,您可以使用它检查与sql的连接。问题将根据提示进行更新。它也无法打开数据库。我不知道EF core,但在EF 6中,我需要
添加迁移SomeName
,然后再
更新数据库
,除非配置了一些自动更新。
添加迁移
本身只创建了用于创建实际更新SQL的中间代码。@grek40-Yo你可以这样回答。那是我的第二个错误!
PM> Add-Migration Initial
Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\Mark\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.0.1-rtm-125 initialized 'MyDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
To undo this action, use Remove-Migration.
namespace MyDb
{
    public class ViewTablesModel : PageModel
    {
        private readonly MyDbContext _context;

        public ViewTablesModel(MyDbContext context)
        {
            _context = context;
        }

        public IList<Blog> Blog { get;set; }

        public async Task OnGetAsync()
        {
            Blog = await _context.Blogs.ToListAsync();
        }
    }
}
C:\Users\Mark>sqllocaldb i MSSQLLocalDB
Nome: "MSSQLLocalDB"
Versione: 13.1.4001.0
Nome condiviso:
Proprietario:              MyComputer\Mark
Creazione automatica:        Sì
Stato:              in esecuzione
Ultima ora di inizio: 22/11/2017 11:45:07
Nome pipe istanza: np:\\.\pipe\LOCALDB#1A9E83A0\tsql\query