5

Code First アプローチで実装されている次の単純なモデルを取得しました。学科とコースは一対多の関係にあります。部門は多くのコースを持つことができますが、コースは 1 つの部門だけに属することができます。モデルはこちら。

public class Department
{
    public int DepartmentId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Course> Courses { get; set; } 
       }

 public class Course
{
    public int CourseId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int DepartmentId { get; set; } 
    public virtual Department Department { get; set; }

}

私の問題は、それらをシードしたいということです。Seed 関数に少なくとも 5 つの値が必要です。ここにシード関数があります。

public class DataInitializer : DropCreateDatabaseIfModelChanges<StudentRecordContext>
{
    protected override void Seed(StudentRecordContext context)
{
    var departments = new  List<Department>
     {
        new Department {  DepartmentId = 1, Title = "English", Description ="English     Department",  Courses = new List<Course>() },
        new Department {  DepartmentId= 2,Title = "Chemistry",   Description ="chemistry department", Courses = new List<Course>() },
        new Department {  DepartmentId= 3,Title = "Mahematics", Description ="mathematics department", Courses = new List<Course>() },
        new Department {  DepartmentId= 4,Title = "Philosophy",  Description ="philosophy department", Courses = new List<Course>() },
        new Department {  DepartmentId= 5,Title = "Biology",     Description ="biology department", Courses = new List<Course>() }
    };                                                           
    departments.ForEach( t => context.Departments.Add(t));
    context.SaveChanges();


    var courses = new List<Course>
    {
        new Course { CourseId = 1055, Title = "Classic English",  Description = "Some        Description", DepartmentId = 1 },
        new Course { CourseId = 2055, Title = "Applied Chemistry",  Description = "Some Description", DepartmentId = 2 },
        new Course { CourseId = 2056, Title = "Applied Mathematics", Description = "Some Description", DepartmentId = 3 },
        new Course { CourseId = 3041, Title = "MetaPhysics",  Description = "Some Description", DepartmentId = 4 },
        new Course { CourseId = 3024, Title = "Molecular Biology", Description = "Some Description", DepartmentId = 5 },
    };
    courses.ForEach(t => context.Courses.Add(t));
    context.SaveChanges();

しかし、これは機能しません。私はEFとCode Firstを初めて使用しています...そして締め切りが迫っています... DBをシードする正しい方法を教えてください。

4

3 に答える 3

3

Seed()メソッドに主キーを設定しないでください。Entity Framework は、名前に Id を含むプロパティが主キーになることを認識します。

Seed()メソッドで次のことを試してください。

protected override void Seed(StudentRecordContext context)
{
    var departments = new  List<Department>
    {
        // this will have DepartmentId = 1
        new Department { Title = "English", Description ="English Department",  Courses = new List<Course>() },
        // more departments
    };
    departments.ForEach(d => context.Departments.Add(d));
    context.SaveChanges();

    var courses = new List<Course>
    {
        // this will have CourseId = 1
        new Course { Title = "Classic English",  Description = "Some Description", Department = departments.FirstOrDefault(d => d.DepartmentId == 1) },
        // this will have CourseId = 2
        new Course { Title = "Drama",  Description = "Some Description", Department = departments.FirstOrDefault(d => d.DepartmentId == 1) },
        // both of the above courses will be added to Department with DepartmentId = 1
        // more courses
    };
    courses.ForEach(c => context.Courses.Add(c));
    context.SaveChanges();

    // now add the two courses to the department
    departments[0].Courses.Add(courses[0]); // in list departments at index 0 add course from list courses at index 0
    departments[0].Courses.Add(courses[1]); // in list departments at index 0 add course from list courses at index 1
    context.SaveChanges();
}
于 2013-03-27T23:50:24.450 に答える
0

これが私が行う方法です。「親」の前に「子」を初期化するだけで、SaveChanges() は必要ありません。

var choices = new List<Choices>
{
    new Choices { Description = "Choice1" },
    new Choices { Description = "Choice2" },
    new Choices { Description = "Choice3" },
    new Choices { Description = "Choice4" }
};

choices.ForEach(c => context.Choices.AddOrUpdate(c));

context.Questions.AddOrUpdate(
    p => p.ID,
    new Question { QuestionText = "Question ?", Choices = choices }
    );  
于 2016-04-12T16:44:14.620 に答える
0

自分で主キーを生成している [DatabaseGenerated(DatabaseGenerationOption.None)] ので、Entity フレームワークがキーを生成していることを認識できるように、CourseId と DepartmentId の属性を配置する必要があります。

より良いオプションは、主キーの EntityFramework に依存し続けることですが、メソッドを少し変更することです

   var courses = new List<Course>
        {
            new Course {Title = "Classic English",  Description = "Some        Description", Department=
     new Department {   Title = "English", Description ="English    
Department",  Courses = new List<Course>() }},
           .....
        };
        courses.ForEach(t => context.Courses.Add(t));

そのため、コースを保存する必要があります。部門を関連付けているため、暗黙的に保存されます。

締め切り頑張ってください!

于 2013-03-26T02:34:48.293 に答える