实体框架共享POCO

时间:2017-05-15 11:51:48

标签: c# entity-framework

我有一点问题,我需要知道EF的最佳设计是什么。 我有一个属性模型:

public enum TargetType
{
    Attributes,
    Availability,
    Master
}

public class Attribute
{
    public int Id { get; set; }
    public int CriteriaId { get; set; }
    [Required, MaxLength(100)] public string Name { get; set; }
    [MaxLength(100)] public string Description { get; set; }
    public TargetType Target { get; set; }

    public virtual IList<Formula> Formulas { get; set; }
}

它有一个公式列表:

public class Formula
{
    public int Id { get; set; }

    public int AttributeId { get; set; }

    [Required] 
    [MaxLength(5)] 
    public string Operator { get; set; }

    [Required] 
    [MaxLength(255)] 
    public string Expression { get; set; }

    [MaxLength(100)] 
    public string Field { get; set; }
}

我在DbContext中匹配的是这样的:

modelBuilder.Entity<Attribute>().HasMany(m => m.Formulas).WithOptional().HasForeignKey(m => m.AttributeId);

问题是,我现在需要将Formulas作为另一个类Answer的单独属性,如下所示:

public class Answer
{
    public int Id { get; set; }

    public int QuestionId { get; set; }

    [Required] 
    [MaxLength(255)] 
    public string Text { get; set; }

    public int Order { get; set; }

    public virtual IList<Formula> Formulas { get; set; }
}

问题是FormulaAttributeId作为外键,因此我无法真正使用Formula模型。我必须创建一个新的:

public class AnswerFormula
{
    public int Id { get; set; }

    public int AnswerId { get; set; }

    [Required] 
    [MaxLength(5)] 
    public string Operator { get; set; }

    [Required] 
    [MaxLength(255)] 
    public string Expression { get; set; }

    [MaxLength(100)] 
    public string Field { get; set; }
}

但这似乎违反直觉。所以我的问题是,有没有人遇到过这个?如果是这样,你是如何解决这个问题的?对我来说,当我开始使用视图模型和工厂时,问题会进一步增强。

0 个答案:

没有答案