DbContext建構初始策略 Database.SetInitializer
範例程式碼:
using System.Data.Entity;
namespace MvcShopping.Models
{
public class MvcShoppingContext : DbContext
{
public MvcShoppingContext()
: base("name=DefaultConnection")
{
Database.SetInitializer(new CreateDatabaseIfNotExists<MvcShoppingContext>());
}
public DbSet<ProductCategory> ProductCategories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<OrderHeader> Orders { get; set; }
public DbSet<OrderDetail> OrderDetailItems { get; set; }
}
}
其意思是new 一個「如果資料庫不存在則新增」策略之類別,其型別為MvcShoppingContext
其初始策略有五種分別是
参考資料: