实体框架代码优先以意外方式生成表/键

时间:2018-12-01 06:15:42

标签: c# entity-framework entity-framework-6 ef-code-first

我有很多代表表的类,但是三个让我头疼的是:人员,任务和角色,这是它们的代码:

public class Person : BaseModel
{
        public int Id { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public string Title { get; set; }

        public ICollection<TestEvent> TestEventsLed { get; set; }
        public ICollection<TestEvent> TestEventsCreated { get; set; }
        public ICollection<Program> ProgramsLed { get; set; }
        public ICollection<Task> TasksCreated { get; set; }
        public ICollection<PersonalEvent> PersonalEventsCreated { get; set; }

        public virtual ICollection<Role> RolesHeld { get; set; }
        public virtual ICollection<Task> TasksAssigned { get; set; }
}

public class Role : BaseModel
{
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Person> PeopleWithThisRole { get; set; }
}

public class Task : BaseModel
{
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime SuspenseDatetime { get; set; }
        public DateTime CreatedDatetime { get; set; }
        public int CreatedById { get; set; }
        public bool Completed { get; set; }
        public bool Archived { get; set; }

        public Person CreatedBy { get; set; }

        public virtual ICollection<Person> PeopleAssigned { get; set; }
}

我最终得到的大部分是我想要的东西,除了一些小问题:

Expected:                                       Actual:
- People should have 0 foreign keys, just       - People has 1 FK and 1 extra column out of
  2 many-to-manys for RolesHeld and               nowhere: Task_Id and the FK is for that
  TasksAssigned                                   new column referencing Id in Tasks?

- Task should have 1 foreign key for            - Task has 2 extra columns out of nowhere
  CreatedById linked to a Person                  called Person_Id and Person_Id1 and then
                                                  identical foreign keys attached to them
                                                  (and it has the expected CreatedById FK)

- There should be a RolePersons table           - This part happened correctly and with the 
  with 2 FKs to represent the many-to-many        correct FKs to represent the many-to-many

- There should be a TaskPersons table           - There is no new table at all for this
  with 2 FKs to represent the many-to-many  

奇怪的是,我以相同的方式(例如两个多对多关系)执行了其中一些操作,但结果只有1个正确吗?可以看到我做错了吗?

2 个答案:

答案 0 :(得分:1)

有时默认映射不是我们想要的,因此我们必须明确地向EF说我们需要什么。只需将此方法添加到您的DbContext中,即可按要求工作:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Person>().HasMany(p => p.TasksAssigned).WithMany(t => t.PeopleAssigned);
    modelBuilder.Entity<Person>().HasMany(p => p.TasksCreated).WithRequired(t => t.CreatedBy).WillCascadeOnDelete(false);
}

答案 1 :(得分:0)

实体框架按照约定执行某些操作。

查看您的Task类和Person类

public class Task : BaseModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime SuspenseDatetime { get; set; }
    public DateTime CreatedDatetime { get; set; }
    public int CreatedById { get; set; }
    public bool Completed { get; set; }
    public bool Archived { get; set; }

    public Person CreatedBy { get; set; }

    public virtual ICollection<Person> PeopleAssigned { get; set; }
}


public class Person : BaseModel
{
    public int Id { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }
    public string Title { get; set; }

    public ICollection<TestEvent> TestEventsLed { get; set; }
    public ICollection<TestEvent> TestEventsCreated { get; set; }
    public ICollection<Program> ProgramsLed { get; set; }
    public ICollection<Task> TasksCreated { get; set; }
    public ICollection<PersonalEvent> PersonalEventsCreated { get; set; }

    public virtual ICollection<Role> RolesHeld { get; set; }
    public virtual ICollection<Task> TasksAssigned { get; set; }
}

在任务类中,您将放置Person对象以及Person的集合。我猜这就是造成头痛的原因。

如果您需要它们之间的多对多关系,则不应将此属性放在任务类中

 public Person CreatedById { get; set; }
 public Person CreatedBy { get; set; }

或者如果您需要它们之间的一对多关系,则从Task类中删除此属性

 public virtual ICollection<Person> PeopleAssigned { get; set; }