更新了帖子 我正在尝试使用modelBuilder为我创建链接表。我能够无错误地进行迁移。链接表在sql数据库中创建。但是,当我查询客户的工作时,ChangeOrders仍然会回来Null。我需要查询什么?我的项目没有引用链接表类,所以我迷路了?
原帖: 我正在尝试为我的多对多关系设置链接表。每个工作都可以有多个客户,我创建了一个" JobCustomer"要做到这一点,但回来的唯一数据是链接表上的类ID。
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool? Hidden { get; set; }
public string UserEmail { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
public DbSet<Job> Jobs { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<ChangeOrder> ChangeOrders { get; set; }
public DbSet<JobType> JobTypes { get; set; }
public DbSet<GeoArea> GeoAreas { get; set; }
public DbSet<JobClass> JobClasses { get; set; }
protected override void OnModelCreating
(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Job>()
.HasMany(j => j.Customers)
.WithMany(c => c.Jobs)
.Map(m =>
{
m.ToTable("JobCustomer");
m.MapLeftKey("JobId");
m.MapRightKey("CustomerId");
});
modelBuilder.Entity<Job>()
.HasMany(j => j.ChangeOrders)
.WithMany(c => c.Jobs)
.Map(m =>
{
m.ToTable("JobChangeOrder");
m.MapLeftKey("JobId");
m.MapRightKey("ChangeOrderId");
});
modelBuilder.Entity<Job>()
.HasMany(j => j.GeoAreas)
.WithMany(c => c.Jobs)
.Map(m =>
{
m.ToTable("JobGeoArea");
m.MapLeftKey("JobId");
m.MapRightKey("GeoAreaId");
});
modelBuilder.Entity<Job>()
.HasMany(j => j.JobTypes)
.WithMany(c => c.Jobs)
.Map(m =>
{
m.ToTable("JobJobType");
m.MapLeftKey("JobId");
m.MapRightKey("JobTypeId");
});
modelBuilder.Entity<Job>()
.HasMany(j => j.JobClasses)
.WithMany(c => c.Jobs)
.Map(m =>
{
m.ToTable("JobJobClass");
m.MapLeftKey("JobId");
m.MapRightKey("JobClassId");
});
}
}
public class Job
{
public int JobId { get; set; }
public int JobNumber { get; set; }
public string JobName { get; set; }
public string JobDescription { get; set; }
public ICollection<Customer> Customers { get; set; }
public ICollection<ChangeOrder> ChangeOrders { get; set; }
public ICollection<GeoArea> GeoAreas { get; set; }
public ICollection<JobClass> JobClasses { get; set; }
public ICollection<JobType> JobTypes { get; set; }
}
public List<Job> GetJobs()
{
List<Job> jobs = new List<Job>();
jobs = db.Jobs
.ToList();
return jobs;
}