Entity Framework(code first)数据库初始化

//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;}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public string Index()
        {
            ViewBag.Message = "欢迎使用 ASP.NET MVC!";

            Database.SetInitializer<TestUsersDB>(new DropCreateDatabaseIfModelChanges<TestUsersDB>());
            var getName = 0;
            using(TestUsersDB context = new TestUsersDB()){
            var users = new UserInfo() { userName="xcl",age=23};
            
            context.UserInfoes.Add(users);
            context.SaveChanges();

            getName = (from c in context.UserInfoes where c.age > 0 select c.age).First();
            }
            return getName.ToString();
            //return View(getName);
        }

        public ActionResult About()
        {
            return View();
        }
    }
}


你可能感兴趣的:(Entity Framework(code first)数据库初始化)