标签:
//1.修改模型,重设数据库 using System.Data.Entity; Database.SetInitializer<LisknoveDataContext>(newDropCreateDatabaseIfModelChanges<LisknoveDataContext>()); //2.修改模型,重设数据库,并初始化数据 using System.Data.Entity; public class LisknoveInitializer:DropCreateDatabaseIfModelChanges<LisknoveDataContext> { protected override void Seed(LisknoveDataContext context) { //base.Seed(context); var genre = new List<Genre> { new Genre{ genreName="Marry" }, new Genre{ genreName="Weair" } }; var material = new List<Material> { new Material{ materialName="Stone" }, new Material{ materialName="Water" }, new Material{ materialName="Flooen" } }; var ring = new List<Ring> { new Ring{ ringName="MakeRing", price=17.21M, ringImage="ke.jpg", Genre=genre.Single(g=>g.genreName=="Marry"), Material=material.Single(m=>m.materialName=="Water") }, new Ring{ ringName="ForverRing", price=15.01M, ringImage="for.jpg", Genre=genre.Single(g=>g.genreName=="Marry"), Material=material.Single(m=>m.materialName=="Flooen") } }; //genre.ForEach(g => context.Genres.Add(g)); ring.ForEach(r => context.Rings.Add(r));//this way enough add data to database without genre and material to add to database. } } Database.SetInitializer<LisknoveDataContext>(new LisknoveInitializer());
using System.Collections.Generic; public class UserInfo { public int UserInfoID { get; set; } public string userName { get; set; } public int age { get; set; } public virtual ICollection<Lesson> lessones { get; set; } }
using System.Collections; using System.Collections.Generic; public class Lesson { public int lessonID { get; set; } public string lessonName { get; set; } public string teacherName { get; set; } public virtual UserInfo UserInfo{get;set;} }
using System.Data.Entity; public class TestUsersDB : DbContext { public DbSet<UserInfo> UserInfoes { get; set; } public DbSet<Lesson> Lessons{get;set;} }
Entity Framework(code first)数据库初始化
标签:
原文地址:http://www.cnblogs.com/lvdongjie/p/5561984.html