[英]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.