- 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 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 enough add data to database without genre
- and material to the add to database.
- }
- }
- Database.setinitializer<lisknovedatacontext> (new Lisknoveinitializer ());
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?
- 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.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?
- 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.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?
- Using System.Data.Entity;
- Public class Testusersdb:dbcontext
- {
- Public dbset<userinfo> userinfoes { get; set;}
- Public dbset<lesson> lessons{get; Set;}
- }
Using System.data.entity;public class testusersdb:dbcontext{public dbset<userinfo> userinfoes {get; set;} Public dbset<lesson> Lessons{get;set;}}
[CSharp]View PlainCopyprint?
- 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 = "Welcome to ASP. 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 the 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) database initialization