实体框架通用模型设计需要作为众多查找表的替代

时间:2013-08-16 11:04:46

标签: c# asp.net-mvc entity-framework ef-code-first lookup-tables

我正在使用Entity Framework 5 CodeFirst开始一个新的ASP.NET MVC项目。要开发的应用程序最初将使用许多不同的查找表,其中一些用于可分配给产品的简单属性(在本例中为枪)

E.g。

  • 项目条件(差,平均,良好,优秀等)
  • 枪械定位(左撇子,右撇子,双手等)

这些'分类/属性'表中大约有10个。

此外,还需要'Make'和'Model'。这两个是相关的(因为Make可以有许多不同的模型)。

最后,一些分类表需要彼此相关。例如,某些'Makes'仅产生某些类型的枪('GunTypes'查找表),而一些枪机制('GunMechanisms'查找表)仅与某些枪类型相关。

最初,我为每个实体设计了一个带有Model类的应用程序,实现了处理上述要求的必要关系。但是,因为某些元素需要重新用于此应用程序的非枪版本,所以我被要求摆脱这些元素/类/表中的每一个,而是使用更通用的方法。拥有“类别”和“期限”实体。

例如,我最初使用下面的内容...

public class Category
{
    // A Category would be, for example, 'Gun Condition', 'Gun Orientation', 'Make' etc.
    public int Id { get; set;}
    public string Name { get; set;}

    // A Category will have any number of Terms associated with it.
    public virtual ICollection<Term> Terms { get; set; }
}

public class Term
{
    // A Term would be, for example, 'Excellent Condition', 'Good Condition', 'Left-Handed', 'Right-Handed' etc.
    public int Id { get; set;}
    public string Name { get; set;}

    // A Term belongs to a Category.
    public virtual Category Category { get; set; }
}

public class Gun
{
    // Edited for brevity.
    public int Id { get; set;}
    public string Descrip { get; set;}

    // All of the terms (what were originally properties), e.g. 1 Term in the collection would be 'Excellent Condition' with the Category 'Gun Condition'. A 2nd Term might be 'Left-Handed' with the Category 'Gun Orientation', and so on. 
    public virtual ICollection<Term> Terms { get; set;}
}

但是,如上所述,我需要能够处理:

a)像'Make'和&amp; '模型',它们彼此相关;

b)“多对多”风格场景,其中'Make'仅产生某些'GunType',而'GunType'将与某些'GunMechanism'(例如气枪(枪型))相关既是'Break Barrel'又是'预先充电'枪机制。)

我真的在努力实施这种模型设计,这已被提出作为一项坚定的要求。

0 个答案:

没有答案