【发布时间】:2018-09-18 04:34:52
【问题描述】:
这是我的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("AppDbContext"));
});
services.AddMvc();
}
这是我的 DbContext:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Actor> Actors { get; set; }
public DbSet<Movie> Movies { get; set; }
public DbSet<MovieActor> MovieActors { get; set; }
}
我的 ConnectionString 很好。我真的很想知道为什么我的数据库在我运行这段代码时没有生成?断点在services.AddDbContext 行中命中,但是当我在AppDbContext 中放置断点时,它没有命中。有人可以帮忙吗?我的代码看起来像这样https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro
【问题讨论】:
-
你添加迁移了吗?
-
@IrakliGabisonia 没有。我没有在我的项目中使用任何迁移。
-
对
AddDbContext的调用将不会调用您的AppDbContext构造函数:它在首次从DI 系统请求时被调用。您需要有一个控制器/动作,将其作为依赖项,然后使用通常的 MVC 东西触发该动作运行。
标签: c# entity-framework asp.net-core ef-code-first ef-core-2.0