Entity Framework (code first) database initialization

Source: Internet
Author: User

  1. 1. Modify the model and reset the database
  2. Using System.Data.Entity;
  3. Database.setinitializer<lisknovedatacontext> (Newdropcreatedatabaseifmodelchanges<lisknovedatacontext > ());
  4. 2. Modify the model, reset the database, and initialize the data
  5. using System.Data.Entity;
  6. public class lisknoveinitializer:dropcreatedatabaseifmodelchanges<lisknovedatacontext>
  7. {
  8. protected override void Seed (Lisknovedatacontext context)
  9. {
  10. //base.  Seed (context);
  11. var genre = New list<genre>
  12. {
  13. New genre{
  14. Genrename="Marry"
  15. },
  16. New genre{
  17. Genrename="Weair"
  18. }
  19. };
  20. var material = New list<material>
  21. {
  22. New material{
  23. Materialname="Stone"
  24. },
  25. New material{
  26. Materialname="Water"
  27. },
  28. New material{
  29. Materialname="Flooen"
  30. }
  31. };
  32. var ring = New List<ring>
  33. {
  34. New ring{
  35. Ringname="Makering",
  36. price=17.21m,
  37. ringimage="Ke.jpg",
  38. Genre=genre. Single (g=>g.genrename=="Marry"),
  39. Material=material. Single (m=>m.materialname=="water")
  40. },
  41. New ring{
  42. Ringname="Forverring",
  43. price=15.01m,
  44. ringimage="For.jpg",
  45. Genre=genre. Single (g=>g.genrename=="Marry"),
  46. Material=material. Single (m=>m.materialname=="Flooen")
  47. }
  48. };
  49. //genre. ForEach (g = context.  Genres.add (g));
  50. Ring. ForEach (r = Context. Rings.add (R)); //this enough add data to database without genre
  51. and material to the add to database.
  52. }
  53. }
  54. Database.setinitializer<lisknovedatacontext> (new Lisknoveinitializer ());
  55. 1. Modify the model and reset the database using System.data.entity;database. Setinitializer<lisknovedatacontext> (newdropcreatedatabaseifmodelchanges<lisknovedatacontext> ());//    2. Modify the model, reset the database, and initialize the data using System.Data.Entity; public class Lisknoveinitializer:dropcreatedatabaseifmodelchanges<lisknovedatacontext> {protected Overrid e 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"}, NE            W 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-enough ADD data to database without genre        and material to the add to database. }}database.setinitializer<lisknovedatacontext> (New LiSknoveinitializer ()); 



Example [CSharp]View PlainCopyprint?
  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 PlainCopyprint?
  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 PlainCopyprint?
    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 PlainCopyprint?
    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. Namespace Mvcapplication1.controllers
    8. {
    9. public class Homecontroller:controller
    10. {
    11. public string Index ()
    12. {
    13. Viewbag.message = "Welcome to ASP. mvc!";
    14. Database.setinitializer<testusersdb> (new dropcreatedatabaseifmodelchanges<testusersdb> ());
    15. var getName = 0;
    16. using (testusersdb context = new Testusersdb ()) {
    17. var users = New UserInfo () {username="Xcl", age=23};
    18. Context. Userinfoes.add (users);
    19. Context. SaveChanges ();
    20. GetName = (from the C in context.) Userinfoes where c.age > 0 select c.age).  First ();
    21. }
    22. return getname.tostring ();
    23. //return View (getName);
    24. }
    25. Public ActionResult About ()
    26. {
    27. return View ();
    28. }
    29. }
    30. }

Entity Framework (code first) database initialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.