1

나는이 '씨'방법이 있습니다Entity Framework (코드 우선)의 Seed 메서드에 두 테이블의 데이터를 추가 할 수 있습니까? 내 데이터 영역에서

protected override void Seed(Context context) 
     {  
      User u1 = new User() 
      { 

       UserName = "dayan", 
       Password = "dayan", 
       Role = "Admin" 

      }; 
      User u2 = new User() 
      { 
       UserName = "neranjan", 
       Password = "neranjan", 
       Role = "employee" 
      }; 

      context.Users.Add(u1); 
      context.Users.Add(u2); 
      base.Seed(context);  
     } 

이처럼 내 사용자 테이블의 모습 :이 같은이 씨의 방법에 더 많은 테이블 데이터를 추가 할 필요가 지금 그래서

:

  Examination e = new Examination() 
      { 
       Description = "fromda console", 
       CutOffMark = 1000, 
       QuestionID = new List<Question>() 
       { 
        new Question() 
        { 
         QuestionDes = "Question", 
         Answer1 = "11", 
         Answer2 = "22", 
         Answer3 = "33", 
         Answer4 = "44", 
         Correct = 1 
        } 
       } 
      }; 

이 코드 메서드는 첫 번째 엔티티 프레임 워크를 사용하기 때문에이 메서드가 필요합니다. 데이터베이스를 삭제하고 데이터베이스를 다시 만들려면이 행을 반드시 작성해야합니다. 테이블.

이 문제를 해결하는 방법을 알려주세요. 감사합니다 !! 이 내가 그것을 해결하는 방법입니다

+0

왜 당신은 단지 그것을 당신의 종자 방법에도 넣을 수 없습니까? –

+0

고맙습니다. – Dayan

답변

0

....

public class ContextSeeder : DropCreateDatabaseIfModelChanges<Context> 
{ 
    protected override void Seed(Context context) 
    { 
     Examination e1 = new Examination() 
     { 
      Description = "Science", 
      CutOffMark = 10, 
      QuestionID = new List<Question>() 
      { 
       new Question() 
       { 
        QuestionDes = "What is a data bus?", 
        Answer1 = "It carries a word to or from memory", 
        Answer2 = "It is used to store intermediate data and instructions", 
        Answer3 = "It decodes the instructions", 
        Answer4 = "It contains the instruction that is being executed", 
        Correct = 1 
        //ExaminationID = 1 
       } 
      } 
     }; 
     Examination e2 = new Examination() 
     { 
      Description = "Science", 
      CutOffMark = 10, 
      QuestionID = new List<Question>() 
      { 
       new Question() 
       { 
        QuestionDes = "What is program counter?", 
        Answer1 = "It keeps track of the memory address of the instruction that is to be executed next.", 
        Answer2 = "It is used to store intermediate data and instructions", 
        Answer3 = "It decodes the instructions", 
        Answer4 = "It contains the instruction that is being executed", 
        Correct = 1 
        //ExaminationID = 1 
       } 
      } 
     }; 
     Examination e3 = new Examination() 
     { 
      Description = "Science", 
      CutOffMark = 10, 
      QuestionID = new List<Question>() 
      { 
       new Question() 
       { 
        QuestionDes = "Expand SD RAM.?", 
        Answer1 = "Synchronous Dynamic Random Access Memory.", 
        Answer2 = "It is used to store intermediate data and instructions", 
        Answer3 = "It decodes the instructions", 
        Answer4 = "It contains the instruction that is being executed", 
        Correct = 1 
        //ExaminationID = 1 
       } 
      } 
     }; 
     Examination e4 = new Examination() 
     { 
      Description = "Computer Science", 
      CutOffMark = 40, 
      QuestionID = new List<Question>() 
      { 
       new Question() 
       { 
        QuestionDes = "What is Instruction register?", 
        Answer1 = "Stores a copy of current instruction.", 
        Answer2 = "It is used to store intermediate data and instructions", 
        Answer3 = "It decodes the instructions", 
        Answer4 = "It contains the instruction that is being executed", 
        Correct = 1 
        //ExaminationID = 1 
       } 
      } 
     }; 

     User u1 = new User() 
     { 

      UserName = "dayan", 
      Password = "dayan", 
      Role = "Admin" 

     }; 
     User u2 = new User() 
     { 
      UserName = "neranjan", 
      Password = "neranjan", 
      Role = "employee" 
     }; 

     context.Examinations.Add(e1); 
     context.Examinations.Add(e2); 
     context.Examinations.Add(e3); 
     context.Examinations.Add(e4); 
     context.Users.Add(u1); 
     context.Users.Add(u2); 
     //context.SaveChanges(); 
     base.Seed(context); 

    } 
} 

내가 MVC 함께 일하고 있어요, 그래서 난 내 MVC 폴더에 'Global.asax에'라는이 파일을 발견하고 난이 'SetInitializer을'추가 그것에. 이게 그 모습 이구나 ...

public class MvcApplication : System.Web.HttpApplication 
    { 
     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      WebApiConfig.Register(GlobalConfiguration.Configuration); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
      AuthConfig.RegisterAuth(); 
      **Database.SetInitializer(new ContextSeeder());** 
     } 
    } 

나는 홈 페이지를 런닝했다. 그만큼. 자동 .... 이 내 홈페이지처럼 보이는 방법입니다 ... DB를에 내 데이터 객체를 삽입

@using (Html.BeginForm("Results", "Exam")) 
{ 
    <table> 
     <tr> 
      <td> 
       @grid.GetHtml(columns: new[]{ 
         grid.Column("ID"), 
         grid.Column("QuestionDes"), 
         grid.Column("Answer1"), 
         grid.Column("Answer2"), 
         grid.Column("Answer3"), 
         grid.Column("Answer4") 
        }) 
      </td> 
      <td> 
       @Html.DropDownList("Answer1", new SelectList(ViewBag.sel, "Value", "Text"), new { @id = "1" })  
       @Html.DropDownList("Answer2", new SelectList(ViewBag.sel, "Value", "Text"), new { @id = "2" }) 
       @Html.DropDownList("Answer3", new SelectList(ViewBag.sel, "Value", "Text"), new { @id = "3" }) 
       @Html.DropDownList("Answer4", new SelectList(ViewBag.sel, "Value", "Text"), new { @id = "4" })    

      </td> 
     </tr> 
    </table>  
    <input type="submit" value="Submit"/> 

} 

컨트롤러 :

public ActionResult Examination() 
     { 
IService ser = new Service(); 
     //taking all the list questions passed from the LoadQuestions_ToTheGridview() method 
     List<Entities.Question> list = ser.LoadQuestions_ToTheGridview(); 
     ViewBag.qlist = list; 

     List<Models.SelectedListItems> selList = new List<Models.SelectedListItems>(); 
     selList.Add(new Models.SelectedListItems { Text = "----Select----", Value = "0" }); 
     selList.Add(new Models.SelectedListItems { Text = "Answer 1", Value = "1" }); 
     selList.Add(new Models.SelectedListItems { Text = "Answer 2", Value = "2" }); 
     selList.Add(new Models.SelectedListItems { Text = "Answer 3", Value = "3" }); 
     selList.Add(new Models.SelectedListItems { Text = "Answer 4", Value = "4" }); 

     ViewBag.sel = selList; 


     return View(list); 
     } 

당신을 감사합니다 !!!!