简体   繁体   English

Database.EnsureCreated无法正常工作

[英]Database.EnsureCreated not working

I'm playing around with Entity Framework 7 versions listed below 我正在使用下面列出的Entity Framework 7版本

"EntityFramework.Core": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final"

My DatabaseContext class is very simple 我的DatabaseContext类非常简单

public class WorldContext : DbContext
{
    public DbSet<Trip> Trip { get; set; }
    public DbSet<Stop> Stop { get; set; }

    public WorldContext()
    {
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connectionString = Startup.Configuration["Data:WorldContextConnection"];

        optionsBuilder.UseSqlServer(connectionString);
        base.OnConfiguring(optionsBuilder);
    }
}

I'm experiencing a problem when hitting the Database.EnsureCreated method in the constructor. 在构造函数中单击Database.EnsureCreated方法时遇到问题。 As far as I can understand, this method is supposed to make ensure that the database is created if it does not exist, otherwise it is supposed to do nothing. 据我所知,这种方法应该确保数据库创建(如果数据库不存在),否则应该什么也不做。 When I debug through my DatabaseContext class code I hit the breakpoint set on the Database.EnsureCreated method, when execution continues the breakpoint is hit a second time (This was not expected), so the DatabaseContext constructor is entered into twice for some reason. 当我通过DatabaseContext类代码进行调试时,我遇到了在Database.EnsureCreated方法上设置的断点,当执行继续时,断点再次被击中(这是不希望的),因此出于某种原因,两次将DatabaseContext构造函数输入两次。 On the second hit the following exception is thrown 在第二次命中时,引发以下异常

在此处输入图片说明

Even if the database exist, shouldn't the EnsureCreated method ignore it? 即使数据库存在, EnsureCreated方法也不应该忽略它吗? Am I missing something here? 我在这里想念什么吗?

不要调用确保在您的构造函数中创建的方法,而是在应用启动过程中调用一次

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该把Database.EnsureCreated放在哪里? - Where should I put Database.EnsureCreated? EntityFramework Core Database.EnsureCreated不会创建数据库 - EntityFramework Core Database.EnsureCreated doesn't create database Database.EnsureCreated似乎不是在创建数据库(xamarin.forms) - Database.EnsureCreated does not seem that it's creating a database (xamarin.forms) 如何在Database.EnsureCreated()和EF Xamarin.Forms之后使用Database.Migration() - How to use Database.Migration() after Database.EnsureCreated() EF Xamarin.Forms EF Core Database.EnsureCreated 获取创建表 SQL 并且不向数据库发送请求 - EF Core Database.EnsureCreated get creating table SQL and without sending request to database Database.EnsureCreated EF 7 ASP.NET 5 MVC 6处的堆栈溢出异常 - Stack Overflow exception at Database.EnsureCreated EF 7 ASP.NET 5 MVC 6 如何使用 .NET 6 在 aspnet 核心 Web 应用程序中执行 database.ensurecreated()? - How do you do database.ensurecreated() in aspnet core web application using .NET 6? context.Database.EnsureDeleted() 和 context.Database.EnsureCreated() 失败 - context.Database.EnsureDeleted() and context.Database.EnsureCreated() fails EF Core 2.1中的DbContext.Database.EnsureCreate令人困惑的行为 - Confusing Behavior of DbContext.Database.EnsureCreated in EF Core 2.1 使用 EF context.Database.EnsureCreated 创建 SQLite 虚拟表? - Creating SQLite Virtual Table with EF context.Database.EnsureCreated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM