C# 具有相同表的多个导航多对多关系实体框架
我刚开始使用EF代码,所以我是这个主题的初学者 我正在研究任务管理系统。我正在处理任务和员工之间的多对多关系。有三种关系C# 具有相同表的多个导航多对多关系实体框架,c#,asp.net,.net,asp.net-mvc,entity-framework,C#,Asp.net,.net,Asp.net Mvc,Entity Framework,我刚开始使用EF代码,所以我是这个主题的初学者 我正在研究任务管理系统。我正在处理任务和员工之间的多对多关系。有三种关系 任务员工(此处员工是负责任务的人员) 任务员工(此处员工是任务完成后将收到通知的人员) 任务员工(此处员工是批准任务的人员) 对于这些多对多关系,我有三张表。 到目前为止,我已经尝试过这种方法,我收集的所有记录都是负责的,而不是相关的。对这个方法有什么建议吗?我做错了什么 我用来获取数据的代码是 var tasks = db1.Tasks.Include(t => t.
var tasks = db1.Tasks.Include(t => t.TaskPriority).Include(t=>t.Responsible).Where(t => t.tas_status.Equals(AppGlobals.PENDING) || t.tas_status.Equals(AppGlobals.WAITING));
_
[表格(“任务”)]
公开课任务
{
公共任务()
{
this.Responsible=newhashset();
this.Notified=new HashSet();
this.Approved=新HashSet();
}
[关键]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
公共int tasu id{get;set;}
公共字符串tasu name{get;set;}
公共虚拟ICollection负责人{get;set;}
公共虚拟ICollection已通知{get;set;}
公共虚拟ICollection已批准{get;set;}
}
[表(“员工”)]
公营雇员
{
[关键]
[显示(Name=“员工ID”)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int emp_id{get;set;}
[必需]
[显示(Name=“员工姓名”)]
公共字符串emp_name{get;set;}
分配的公共虚拟ICollection{get;set;}
公共虚拟ICollection已通知{get;set;}
公共虚拟ICollection已批准{get;set;}
}
[表(“任务负责人”)]
公共类任务负责人
{
[必需]
[显示(Name=“任务”)]
[键,列(顺序=0)]
公共int tasu id{get;set;}
[必需]
[显示(Name=“负责人”)]
[键,列(“emp_id”,顺序=1)]
public int emp_id{get;set;}
公共虚拟任务任务{get;set;}
公共虚拟员工负责人{get;set;}
}
[表(“任务批准”)]
公共类任务批准
{
[必需]
[显示(Name=“任务”)]
[键,列(顺序=0)]
公共int tasu id{get;set;}
[必需]
[显示(Name=“Person Approved”)]
[键,列(顺序=1)]
public int emp_id{get;set;}
公共虚拟任务任务{get;set;}
公共虚拟员工已批准{get;set;}
}
[表(“任务通知”)]
公共类任务通知
{
[必需]
[显示(Name=“任务”)]
[键,列(顺序=0)]
公共int tasu id{get;set;}
[必需]
[显示(Name=“通知的人员”)]
[键,列(“emp_id”,顺序=1)]
public int emp_id{get;set;}
公共虚拟任务任务{get;set;}
公共虚拟员工通知{get;set;}
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(a=>a.Responsible)
.有许多(d=>d.任务)
.Map(m=>
{
m、 MapLeftKey(“tas_id”);
m、 MapRightKey(“emp_id”);
m、 ToTable(“任务负责人”);
});
modelBuilder.Entity()
.HasMany(a=>a.Notified)
.有许多(d=>d.任务)
.Map(m=>
{
m、 MapLeftKey(“tas_id”);
m、 MapRightKey(“emp_id”);
m、 ToTable(“任务通知”);
});
modelBuilder.Entity()
.HasMany(a=>a.Approved)
.有许多(d=>d.任务)
.Map(m=>
{
m、 MapLeftKey(“tas_id”);
m、 MapRightKey(“emp_id”);
m、 ToTable(“任务批准”);
});
基于模型创建(modelBuilder);
}
您是否通过fluent API将每个多对多关系映射到各个表?是的,我添加了向下滚动@Stoward94的代码只是一种预感,但您是否尝试注释掉您创建的多对多类并只保留fluent API映射?还不支持@Stoward94每种类型的多个对象集。对象集“Responsible”和“Notified”都可以包含“Polani.Models.Employee”类型的实例。是否将每个多对多关系映射到各个表,通过fluent API?是的,我添加了向下滚动@Stoward94的代码,这只是一种预感,但您是否尝试注释掉您创建的多对多类,只留下fluent API映射?还不支持@Stoward94每种类型的多个对象集。对象集“Responsible”和“Notified”都可以包含“Polani.Models.Employee”类型的实例。
[Table("Task")]
public class Task
{
public Task()
{
this.Responsible = new HashSet<Employee>();
this.Notified = new HashSet<Employee>();
this.Approved = new HashSet<Employee>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int tas_id { get; set; }
public string tas_name {get; set;}
public virtual ICollection<Employee> Responsible { get; set; }
public virtual ICollection<Employee> Notified { get; set; }
public virtual ICollection<Employee> Approved { get; set; }
}
[Table("Employee")]
public class Employee
{
[Key]
[Display(Name = "Employee ID")]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int emp_id { get; set; }
[Required]
[Display(Name = "Employee Name")]
public string emp_name { get; set; }
public virtual ICollection<Task> Assigned { get; set; }
public virtual ICollection<Task> Notified { get; set; }
public virtual ICollection<Task> Approved { get; set; }
}
[Table("TaskResponsible")]
public class TaskResponsible
{
[Required]
[Display(Name = "Task")]
[Key, Column(Order = 0)]
public int tas_id { get; set; }
[Required]
[Display(Name = "Person Responsible")]
[Key, Column("emp_id", Order = 1)]
public int emp_id { get; set; }
public virtual Task Task { get; set; }
public virtual Employee Responsible { get; set; }
}
[Table("TaskApproval")]
public class TaskApproval
{
[Required]
[Display(Name = "Task")]
[Key, Column(Order = 0)]
public int tas_id { get; set; }
[Required]
[Display(Name = "Person Approved")]
[Key, Column(Order = 1)]
public int emp_id { get; set; }
public virtual Task Task { get; set; }
public virtual Employee Approved { get; set; }
}
[Table("TaskNotified")]
public class TaskNotified
{
[Required]
[Display(Name = "Task")]
[Key, Column(Order = 0)]
public int tas_id { get; set; }
[Required]
[Display(Name = "Person Notified")]
[Key, Column("emp_id", Order = 1)]
public int emp_id { get; set; }
public virtual Task Task { get; set; }
public virtual Employee Notified { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Task>()
.HasMany(a => a.Responsible)
.WithMany(d => d.Tasks)
.Map(m =>
{
m.MapLeftKey("tas_id");
m.MapRightKey("emp_id");
m.ToTable("TaskResponsible");
});
modelBuilder.Entity<Task>()
.HasMany(a => a.Notified)
.WithMany(d => d.Tasks)
.Map(m =>
{
m.MapLeftKey("tas_id");
m.MapRightKey("emp_id");
m.ToTable("TaskNotified");
});
modelBuilder.Entity<Task>()
.HasMany(a => a.Approved)
.WithMany(d => d.Tasks)
.Map(m =>
{
m.MapLeftKey("tas_id");
m.MapRightKey("emp_id");
m.ToTable("TaskApproval");
});
base.OnModelCreating(modelBuilder);
}