我正在尝试使用实体框架来构建数据库,我需要一些模型的帮助:
模型包括:公司,部门,用户,TaskType1,TaskType2,TaskType3
public class Company
{
public Company()
{
this.Departments = new HashSet<Department>();
}
public int Id { get; set;}
public string CompanyName { get; set;}
public string Address { get; set;}
public string Phone { get; set;}
public virtual ICollection<Department> Departments { get; set;}
}
public class Department
{
public Department()
{
this.Users = new HashSet<User>();
}
public int Id { get; set;}
public string Name { get; set;}
public virtual Company Company { get; set;}
public virtual ICollection<User> Users { get; set;}
}
public class User
{
public Company()
{
}
public int Id { get; set;}
public string UserName { get; set;}
public string FullName { get; set;}
public virtual Department Department { get; set;}
}
public class TaskType1
{
public TaskType1()
{
}
public int Id { get; set;}
public string Name { get; set;}
public string Description { get; set;}
public int Priority { get; set;}
}
public class TaskType2
{
public TaskType2()
{
}
public int Id { get; set;}
public string Name { get; set;}
public string Description { get; set;}
public int Priority { get; set;}
public double EstimatedCosts { get; set;}
}
public class TaskType3
{
public TaskType3()
{
}
public int Id { get; set;}
public string Name { get; set;}
public string Description { get; set;}
public int Priority { get; set;}
public bool IsDone { get; set;}
}
我需要将TaskType1,TaskType2,TaskType3作为表的原因是因为有不同类型的任务(需要不同的数据/字段)。
如何将我的任务类型连接到公司以及部门和用户,以便获得以下结果:
P.S。任务类型具有共同和不同的列
答案 0 :(得分:0)
您需要在模型之间建立关系。例如,要获取每个公司X的所有任务,您需要在每个Tasks表中使用companyID列,并将其作为Company表的外键。然后,您可以在公司模型中设置导航属性:
public virtual ICollection<TaskType1> TasksType1 { get; set; }
你在这里使用ICollection,因为公司可以有多个任务......如果每个公司只能有一个任务,你可以这样做:
public virtual TaskType1 TaskType1 { get; set; }
这使您可以像这样访问任务:
companies = db.Companies.Include(i => i.TasksType1);
你看过Contoso大学的教程吗?它对建立层次模型和相关关系有很好的介绍。
(这是MVC版本,如果您愿意的话,还有一个WebForms版本的教程)