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

发布于 2024-11-03 06:18:12 字数 5319 浏览 6 评论 0原文

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

    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= "[email protected]", FirstName= "Asif", LastName="Hameed" },
     new Person { Id= 2 , Age= 10, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed"  },
     new Person { Id= 3 , Age= 10, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed"  },
     new Person { Id= 4 , Age= 10, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed"  }};

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

        return View(db.Tasks);
    }

我的数据库上下文如下所示:

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 表,但不会创建联合表 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= "[email protected]", FirstName= "Asif", LastName="Hameed", Tasks= new List<Task> { t1,t2 }},
         new Person { Id= 2 , Age= 30, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t1}  },
         new Person { Id= 3 , Age= 29, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t3,t2 } },
         new Person { Id= 4 , Age= 35, EmailAddress= "[email protected]", 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.

I am initializing the entities like this:

    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= "[email protected]", FirstName= "Asif", LastName="Hameed" },
     new Person { Id= 2 , Age= 10, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed"  },
     new Person { Id= 3 , Age= 10, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed"  },
     new Person { Id= 4 , Age= 10, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed"  }};

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

        return View(db.Tasks);
    }

my db context looks like this:

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

I want to assign tasks to persons how can I do this ?

My model looks like this :

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; }
}

When I run my mvc application, Person and Task tables are created but juntion table PersonTask not created.

[EDIT]

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= "[email protected]", FirstName= "Asif", LastName="Hameed", Tasks= new List<Task> { t1,t2 }},
         new Person { Id= 2 , Age= 30, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t1}  },
         new Person { Id= 3 , Age= 29, EmailAddress= "[email protected]", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t3,t2 } },
         new Person { Id= 4 , Age= 35, EmailAddress= "[email protected]", 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

I assume that person table should have tasks not the task table should have person Id

Please suggest solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-11-10 06:18:12

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

如果您想将 Task 分配给 Person,只需运行:

person.Tasks.Add(task);

Junction table will not be created because you have defined one-to-many relation not many-to-many. The Task entity in your model can be assigned only to single Person not to many persons.

If you want to assing Task to Person simply run:

person.Tasks.Add(task);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文