我正在尝试使用代码优先创建数据库项目和数据注释,但我对架构有一些疑问。我不确定我的关系模式是否有意义,在SQL中是否正确并且可能在EF中实现。如何从一个子表到两个parrent表之一建立条件关系。 在我的ProcessRelation表中,我将保存两个关系。 JobBusinessArea - Estimates和jobBussinesArea - PriceList。我希望它能有所作为。感谢您提供任何帮助和建议。
[Table("ProcessRelation")]
public class ProcessRelation
{
[Key, Column("AreaId", Order = 0)]
public int AreaId { get; set; }
[Key, Column("ModId", Order = 1)]
public int ModId { get; set; }
[Key, Column("PriceSourceTypeId", Order = 2)]
public int PriceSourceTypeId { get; set; }
public int CrtUsrnm { get; set; }
public DateTime CrtTmstmp { get; set; }
public int LcUsrnm { get; set; }
public DateTime LcTmstmp { get; set; }
[ForeignKey("PriceSourceTypeId")]
public virtual BillingProjectCode PriceSourceTypeCode { get; set; }
[ForeignKey("AreaId")]
public virtual JobBusinessArea JobBusinessArea { get; set; }
//TODO:
//Depending on the PriceSourceTypeId i would like create reference to PriceListProcessMod Table
//or EstimateProcessMod Table
//[ForeignKey("ModId")]
//public virtual PriceListProcessMod PriceListProcessMod { get; set; }
// or
//public virtual EstimateProcessMod EstimateProcessMod { get; set; }
}
[Table("PriceListProcessMod")]
public class PriceListProcessMod
{
[Key, Column("ModId", Order = 0)]
public int ModId { get; set; }
public int ProcessId { get; set; }
public int Quantity { get; set; }
public bool IsIncluded { get; set; }
public int CrtUsrnm { get; set; }
public DateTime CrtTmstmp { get; set; }
public int LcUsrnm { get; set; }
public DateTime LcTmstmp { get; set; }
public decimal? CommisionPercentage { get; set; }
public bool? IsProportionalyFixed { get; set; }
[ForeignKey("ProcessId")]
public virtual PriceListProcess PriceListProcess { get; set; }
}
[Table("EstimateProcessMod")]
public class EstimateProcessMod
{
[Key, Column("ModId", Order = 0)]
public int ModId { get; set; }
public int? SplitId { get; set; }
public int ProcessId { get; set; }
public int Quantity { get; set; }
public bool IsIncluded { get; set; }
public int CrtUsrnm { get; set; }
public DateTime CrtTmstmp { get; set; }
public int LcUsrnm { get; set; }
public DateTime LcTmstmp { get; set; }
public decimal? CommisionPercentage { get; set; }
public bool? IsProportionalyFixed { get; set; }
[ForeignKey("ProcessId")]
public virtual EstimateProcess EstimateProcess{ get; set; }
}
[Table("JobBusinessArea")]
public class JobBusinessArea
{
[Key, Column("JobBusinessAreaId", Order = 0)]
public int JobBusinessAreaId { get; set; }
public int CrtUsrnm { get; set; }
public DateTime CrtTmstmp { get; set; }
public int LcUsrnm { get; set; }
public DateTime LcTmstmp { get; set; }
public int ProjectId { get; set; }
public int AreaTypeId { get; set; }
public int SourceId { get; set; }
}
答案 0 :(得分:0)
似乎EstimateProcessMod和PriceListProcessMod有很多共同之处。我建议利用它并将两个实体存储在单个表ProcessMod中。通过创建ProcessMod抽象类并使用Table attribute
注释它。具体类只定义差异。
[Table("ProcessMod")]
public abstract class ProcessMod
{
[Key, Column("ModId", Order = 0)]
public int ModId { get; set; }
public int ProcessId { get; set; }
public int Quantity { get; set; }
public bool IsIncluded { get; set; }
public int CrtUsrnm { get; set; }
public DateTime CrtTmstmp { get; set; }
public int LcUsrnm { get; set; }
public DateTime LcTmstmp { get; set; }
public decimal? CommisionPercentage { get; set; }
public bool? IsProportionalyFixed { get; set; }
}
public class EstimateProcessMod : ProcessMod
{
public int? SplitId { get; set; }
[ForeignKey("ProcessId")]
public virtual EstimateProcess EstimateProcess{ get; set; }
}
public class PriceListProcessMod : ProcessMod
{
[ForeignKey("ProcessId")]
public virtual PriceListProcess PriceListProcess { get; set; }
}
[Table("ProcessRelation")]
public class ProcessRelation
{
[Key, Column("AreaId", Order = 0)]
public int AreaId { get; set; }
[Key, Column("ModId", Order = 1)]
public int ModId { get; set; }
[Key, Column("PriceSourceTypeId", Order = 2)]
public int PriceSourceTypeId { get; set; }
public int CrtUsrnm { get; set; }
public DateTime CrtTmstmp { get; set; }
public int LcUsrnm { get; set; }
public DateTime LcTmstmp { get; set; }
[ForeignKey("PriceSourceTypeId")]
public virtual BillingProjectCode PriceSourceTypeCode { get; set; }
[ForeignKey("AreaId")]
public virtual JobBusinessArea JobBusinessArea { get; set; }
[ForeignKey("ModId")]
public virtual ProcessMod ProcessMod { get; set; }
}
您可能可以在ProcessRelation类中删除PriceSourceTypeId并将其移至ProcessMod类并使其成为鉴别器,或者您可能根本不需要使用它。这实际上取决于应用程序的其余部分。
您可能会对Table per Hierarchy感兴趣,了解其工作原理。