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


    1. //1.修改模型,重设数据库  
    2. using System.Data.Entity;  
    3.   
    4. Database.SetInitializer<LisknoveDataContext>(newDropCreateDatabaseIfModelChanges<LisknoveDataContext>());  
    5.   
    6. //2.修改模型,重设数据库,并初始化数据  
    7.     using System.Data.Entity;  
    8.   
    9.     public class LisknoveInitializer:DropCreateDatabaseIfModelChanges<LisknoveDataContext>  
    10.     {  
    11.         protected override void Seed(LisknoveDataContext context)  
    12.         {  
    13.             //base.Seed(context);  
    14.             var genre = new List<Genre>  
    15.             {  
    16.                 new Genre{  
    17.                     genreName="Marry"  
    18.                 },  
    19.                 new Genre{  
    20.                     genreName="Weair"  
    21.                 }  
    22.             };  
    23.   
    24.             var material = new List<Material>  
    25.             {  
    26.                 new Material{  
    27.                     materialName="Stone"  
    28.                 },  
    29.                 new Material{  
    30.                     materialName="Water"  
    31.                 },  
    32.                 new Material{  
    33.                     materialName="Flooen"  
    34.                 }  
    35.             };  
    36.   
    37.             var ring = new List<Ring>  
    38.             {  
    39.                 new Ring{  
    40.                     ringName="MakeRing",  
    41.                     price=17.21M,  
    42.                     ringImage="ke.jpg",  
    43.                     Genre=genre.Single(g=>g.genreName=="Marry"),  
    44.                     Material=material.Single(m=>m.materialName=="Water")  
    45.                 },  
    46.                 new Ring{  
    47.                     ringName="ForverRing",  
    48.                     price=15.01M,  
    49.                     ringImage="for.jpg",  
    50.                     Genre=genre.Single(g=>g.genreName=="Marry"),  
    51.                     Material=material.Single(m=>m.materialName=="Flooen")  
    52.                 }  
    53.             };  
    54.   
    55.             //genre.ForEach(g => context.Genres.Add(g));  
    56.             ring.ForEach(r => context.Rings.Add(r));//this way enough add data to database without genre   
    57.                                                                              and material to add to database.  
    58.         }  
    59.     }  
    60.   
    61. Database.SetInitializer<LisknoveDataContext>(new LisknoveInitializer());  
    62. //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());



    示例
    [csharp] view plain copy
     
    print?
    1. using System.Collections.Generic;  
    2. public class UserInfo  
    3. {  
    4.     public int UserInfoID { get; set; }  
    5.     public string userName { get; set; }  
    6.     public int age { get; set; }  
    7.     public virtual ICollection<Lesson> lessones { get; set; }  
    8. }  
    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; }
    }
    [csharp] view plain copy
     
    print?
    1. using System.Collections;  
    2. using System.Collections.Generic;  
    3. public class Lesson {  
    4.     public int lessonID { get; set; }  
    5.     public string lessonName { get; set; }  
    6.     public string teacherName { get; set; }  
    7.     public virtual UserInfo UserInfo{get;set;}  
    8. }  
    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;}
    }
    [csharp] view plain copy
     
    print?
    1. using System.Data.Entity;  
    2. public class TestUsersDB : DbContext  
    3. {  
    4.     public DbSet<UserInfo> UserInfoes { get; set; }  
    5.     public DbSet<Lesson> Lessons{get;set;}  
    6. }  
    using System.Data.Entity;
    public class TestUsersDB : DbContext
    {
        public DbSet<UserInfo> UserInfoes { get; set; }
        public DbSet<Lesson> Lessons{get;set;}
    }
    [csharp] view plain copy
     
    print?
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Web;  
      5. using System.Web.Mvc;  
      6. using System.Data.Entity;  
      7.   
      8. namespace MvcApplication1.Controllers  
      9. {  
      10.     public class HomeController : Controller  
      11.     {  
      12.         public string Index()  
      13.         {  
      14.             ViewBag.Message = "欢迎使用 ASP.NET MVC!";  
      15.   
      16.             Database.SetInitializer<TestUsersDB>(new DropCreateDatabaseIfModelChanges<TestUsersDB>());  
      17.             var getName = 0;  
      18.             using(TestUsersDB context = new TestUsersDB()){  
      19.             var users = new UserInfo() { userName="xcl",age=23};  
      20.               
      21.             context.UserInfoes.Add(users);  
      22.             context.SaveChanges();  
      23.   
      24.             getName = (from c in context.UserInfoes where c.age > 0 select c.age).First();  
      25.             }  
      26.             return getName.ToString();  
      27.             //return View(getName);  
      28.         }  
      29.   
      30.         public ActionResult About()  
      31.         {  
      32.             return View();  
      33.         }  
      34.     }  
      35. }  
  • 相关阅读:
    Spring Boot 7:配置文件信息读取
    Spring Boot 6:自定义filter
    Spring AOP:Java动态代理和CGlib
    Spring AOP:概念
    设计模式(二)---策略模式
    设计模式(一)--装饰模式
    java并发编程实战笔记
    剑指offer java -查找旋转数组的最小数字
    读书计划
    协议初学
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/5561984.html
Copyright © 2020-2023  润新知