いくつかのクラスからデータベースを生成するためにEntity Framework 4.3を使用しています。私は、Deals
のコレクションを持っているItem
クラスを持っています。これは、無料でも割引でもかまいません。私のクラスはそうのようにモデル化されていますJSONとしての派生クラスの使用
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呼び出しを使用していますが、別々の取引をどのように扱うかはわかりません。 Item
を取得した場合、3 Deals
と表示されている場合、2 Free
と1 Discounted
どうすれば対応できますか?私のアイテムクラスに2 Collections
(Free
の場合は1、Discounted
の場合は1)が必要ですか? 1つのフラグを持つDeal
クラスを持っていることに戻りますか?