确保使用EF7自动创建数据库(如果它不存在)的最佳方法是什么?我读到你不应该把Database.EnsureCreated放在你的 DatbaseContext构造函数因此不确定如何正确连接它。
以下是OdeToFood / Startup.cs类(asp.net core 1.0.1 web app)的ConfigureService方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton(Configuration);
services.AddSingleton<IGreeter, Greeter>();
services.AddScoped<IRestaurantData, SqlRestaurantData>();
services.AddDbContext<OdeToFoodDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("OdeToFood")));
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<OdeToFoodDbContext>();
}
public class OdeToFoodDbContext : IdentityDbContext<User>
{
public OdeToFoodDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Restaurant> Restaurants { get; set; }
}