/// <summary>
/// 说明:
/// 创建日期:2016/9/30 14:49:48
/// 创建人:曹永承
/// </summary>
public class AppDbContext:DbContext
{
private AppDbContext() : base("db")
{
Database.SetInitializer<AppDbContext>(new AppDbInit());
} /// <summary>
/// 创建唯一的AppDbContext
/// </summary>
/// <returns></returns>
public static AppDbContext CreateOnly()
{
AppDbContext dbContext = CallContext.GetData("dbContext") as AppDbContext;
if (dbContext == null)
{
dbContext = new AppDbContext();
CallContext.SetData("dbContext", dbContext);
}
return dbContext;
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove(
new PluralizingTableNameConvention()); //移除在数据库中表名复数约束
base.OnModelCreating(modelBuilder);
} public DbSet<AccountDetial> AccountDetails { get; set; }
} public class AppDbInit : DropCreateDatabaseIfModelChanges<AppDbContext>
{
protected override void Seed(AppDbContext context)
{
init(context);
base.Seed(context);
} protected void init(AppDbContext context)
{
var list = new List<AccountDetial>
{
new AccountDetial { Account="a",Age=, Password=""},
new AccountDetial { Account="b",Age=, Password=""},
new AccountDetial { Account="c",Age=, Password=""},
new AccountDetial { Account="d",Age=, Password=""},
new AccountDetial { Account="e",Age=, Password=""}
}; foreach (AccountDetial account in list)
{
context.AccountDetails.Add(account);
}
context.SaveChanges();
}
}