我似乎对使用 EF 5.0 CodeFirst 的以下模型有问题:
当我尝试使用 SalesPlanActivity 进行列表时,我无法从导航属性中获取活动名称
即,如果我的模型是 SalesPlanActivity,model.Activity <--- 为空,但 model.Company 和 model.SalesPlan 没问题。
这个想法是一个公司有一个活动库,每个公司都有一些销售计划,每个销售计划都有一些活动(来自活动库)
我映射不正确吗?
SalesPlanActivity 和 Activity 之间的映射应该是 1:1 吗?
我添加了以下关系
// Relationships
modelBuilder.Entity<SalesPlanActivity>().HasRequired(t => t.Activity)
.WithMany(t => t.SalesPlanActivities)
.HasForeignKey(d => new { d.CompanyId, d.ActivityId });
modelBuilder.Entity<SalesPlanActivity>().HasRequired(t => t.SalesPlan)
.WithMany(t => t.SalesPlanActivities)
.HasForeignKey(d => new { d.SalesPlanId, d.CompanyId });
public class Activity : IAuditable
{
[Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ActivityId { get; set; }
[Key, Column(Order = 1)]
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
[Display(Name = "Activity Name"), Required, StringLength(256)]
public string Name { get; set; }
public virtual ICollection<SalesPlanActivity> SalesPlanActivities { get; set; }
[DataType(DataType.DateTime)]
public DateTime CreatedOn { get; set; }
public Guid CreatedBy { get; set; }
[DataType(DataType.DateTime)]
public DateTime ModifiedOn { get; set; }
public Guid ModifiedBy { get; set; }
}
public class Company : IAuditable
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CompanyId { get; set; }
[Display(Name = "Company Name"), Required, StringLength(256)]
public string Name { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
public virtual ICollection<SalesPlan> SalesPlans { get; set; }
[DataType(DataType.DateTime)]
public DateTime CreatedOn { get; set; }
public Guid CreatedBy { get; set; }
[DataType(DataType.DateTime)]
public DateTime ModifiedOn { get; set; }
public Guid ModifiedBy { get; set; }
}
public class SalesPlan : IAuditable
{
[Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SalesPlanId { get; set; }
[Key, Column(Order = 1)]
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
[Display(Name = "Sales Plan Name"), Required, StringLength(256)]
public string Name { get; set; }
[Display(Name = "Total Points"), Required]
public decimal TotalPoints { get; set; }
public virtual ICollection<SalesPlanActivity> SalesPlanActivities { get; set; }
[DataType(DataType.DateTime)]
public DateTime CreatedOn { get; set; }
public Guid CreatedBy { get; set; }
[DataType(DataType.DateTime)]
public DateTime ModifiedOn { get; set; }
public Guid ModifiedBy { get; set; }
}
public class SalesPlanActivity : IAuditable
{
[Key, Column(Order = 0)]
public int SalesPlanId { get; set; }
public virtual SalesPlan SalesPlan { get; set; }
[Key, Column(Order = 1)]
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
[Key, Column(Order = 2)]
public int ActivityId { get; set; }
public virtual Activity Activity { get; set; }
public decimal Points { get; set; }
public int SortOrder { get; set; }
[DataType(DataType.DateTime)]
public DateTime CreatedOn { get; set; }
public Guid CreatedBy { get; set; }
[DataType(DataType.DateTime)]
public DateTime ModifiedOn { get; set; }
public Guid ModifiedBy { get; set; }
}