实体框架中的一对多关系4.1代码优先

时间:2011-04-24 13:35:57

标签: asp.net-mvc-3 mapping entity-relationship code-first entity-framework-4.1

我正在初始化这样的实体:

    TasksListDB db = new TasksListDB();

    public ActionResult Index()
    {

        var tasks1 = new List<Task>()
        {
            new Task { Id=1, Name = "Task 1", Difficulty = 1, DateCreated = DateTime.Parse("1/12/2011") , IsDone= true },
            new Task { Id=2, Name = "Task 2", Difficulty = 2, DateCreated = DateTime.Parse("11/2/2011") , IsDone = false}               
        };

        var tasks2 = new List<Task>()
        {
             new Task { Id=3, Name = "Task 3", Difficulty = 3, DateCreated = DateTime.Parse("11/2/2011") , IsDone = false},
            new Task { Id=4, Name = "Task 4", Difficulty = 5, DateCreated = DateTime.Parse("1/2/2010") , IsDone= true }
        };

        tasks1.ForEach(t => db.Tasks.Add(t));
        tasks2.ForEach(t => db.Tasks.Add(t));

        var Persons = new List<Person> { 
     new Person { Id= 1 , Age= 10, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed" },
     new Person { Id= 2 , Age= 10, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed"  },
     new Person { Id= 3 , Age= 10, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed"  },
     new Person { Id= 4 , Age= 10, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed"  }};

        Persons.ForEach(p => db.Persons.Add(p));

        return View(db.Tasks);
    }

我的db上下文如下:

public class TasksListDB : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public DbSet<Task> Tasks { get; set; }   
}

我想将任务分配给人员我该怎么做?

我的模型看起来像这样:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }        
    public string EmailAddress { get; set; }        
    public virtual ICollection<Task> Tasks { get; set; }
}

public class Task
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Required]
    public DateTime DateCreated { get; set; }
    [Required]
    [Range(1,5,ErrorMessage="Difficulty must be between 1 and 5")]
    public int Difficulty { get; set; }
    public bool IsDone { get; set; }

    public virtual Person Person { get; set; }
}

当我运行我的mvc应用程序时,会创建Person和Task表,但不会创建juntion表PersonTask。

[编辑]

I am assigning tasks like this in initializer class:

   var t1 = new Task { Id = 1, Name = "Urgent Task", DateCreated = DateTime.Now, Difficulty = 2, IsDone = true };
            var t2 = new Task { Id = 2, Name = "Business Task", DateCreated = DateTime.Now, Difficulty = 1, IsDone = true };
            var t3 = new Task { Id = 3, Name = "Home Task", DateCreated = DateTime.Now, Difficulty = 2, IsDone = false };

 context.Tasks.Add(t1);
            context.Tasks.Add(t2);
            context.Tasks.Add(t3);

    var Persons = new List<Person> { 
         new Person { Id= 1 , Age= 40, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed", Tasks= new List<Task> { t1,t2 }},
         new Person { Id= 2 , Age= 30, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t1}  },
         new Person { Id= 3 , Age= 29, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t3,t2 } },
         new Person { Id= 4 , Age= 35, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t1,t3 } }};

 context.Persons.Add(Persons[0]);
            context.Persons.Add(Persons[1]);
            context.Persons.Add(Persons[2]);
            context.Persons.Add(Persons[3]);

            context.SaveChanges();

but when i see the task table in sql server, it looks like this:

Id  Name          DateCreated          Difficulty   IsDone  Person_Id
1   Urgent Task 2011-04-24 19:32:03.990 2   1   4
2   Business Task   2011-04-24 19:32:03.990 1   1   3
3   Home Task   2011-04-24 19:32:03.990 2   0   4

我假设人员表应该有任务而不是任务表应该有人ID

Please suggest solution.

1 个答案:

答案 0 :(得分:0)

不会创建连接表,因为您已经定义了一对多关系而不是多对多关系。模型中的Task实体只能分配给单个Person,而不能分配给很多人。

如果您想Task Personperson.Tasks.Add(task); 分享,请执行以下操作:

{{1}}