我正在使用 Entity Framework 4.3 从某些类生成我的数据库。我有一Item
门课,里面有一系列Deals
可以免费或打折的课程。我的课程是这样建模的:
public class Item
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
//Other properties
public virtual ICollection<Deal> Deals { get; set; }
}
public abstract class Deal
{
//These properties are shared between various Deals
public int Id { get; set; }
public bool Monday { get; set; }
public bool Tuesday { get; set; }
public bool Wednesday { get; set; }
public bool Thursday { get; set; }
public bool Friday { get; set; }
public bool Saturday { get; set; }
public bool Sunday { get; set; }
public TimeSpan TimeFrom { get; set; }
public TimeSpan TimeTo { get; set; }
public int AgesFrom { get; set; }
public int AgesTo { get; set; }
public string Comments { get; set; }
public virtual ICollection<Vote> Votes { get; set; }
}
public class DiscountedDeal : Deal
{
public double PriceAfterDiscount { get; set; }
}
public class FreeDeal : Deal
{
//No properties needed here
}
然后在我的onModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<FreeDeal>().ToTable("FreeDeals");
modelBuilder.Entity<DiscountedDeal>().ToTable("DiscountedDeals");
}
我最初有一个Deal
带有布尔字段的类来表示它是否免费。我现在拥有它的方式似乎更加规范化,但自然会使事情变得更加复杂。
在前端,我使用 ajax 调用来发送和检索这些数据,但我不确定如何处理单独的交易。如果我检索 anItem
并且它有 3 Deals
,比如 2Free
和 1Discounted
我该如何处理?我的项目类别中应该有 2 个Collections
(1 个Free
和 1 个)吗?Discounted
我应该回到一个Deal
有旗帜的班级吗?