无法在EF 6上使用Fluent API创建TPH

时间:2017-10-02 13:45:49

标签: c# entity-framework ef-fluent-api tph

我为类似Google表格的项目建模。波纹管实体非常简单明了(我猜),如下所示。

问题类型:

size="tiny"

答案类型:

// Base class for any kind of question
public abstract class Question : Bean
{
    public string Statement { get; set; }
}

// Visual questions are questions where images are answers.
public class VisualQuestion : Question
{
    public virtual VisualAnswer Answer { get; set; }
}

// Discursive questions are questions where big texts are answers.
public class DiscursiveQuestion : Question
{
    public virtual DiscursiveAnswer Answer { get; set; }
}

// Objective questions are questions that can have multiple answers,
// where each of them should be no bigger than 1 character.
public class ObjectiveQuestion : Question
{
    public virtual List<ObjectiveQuestionOption> Options { get; set; }
}

// Options for objective questions.
public class ObjectiveQuestionOption : Question
{
    public int ObjectiveQuestionId { get; set; }

    public virtual ObjectiveQuestion Question { get; set; }

    public virtual ObjectiveAnswer Answer { get; set; }
}

Bean在哪里:

public abstract class Answer : Bean
{
    public int QuestionId { get; set; }
}

public class DiscursiveAnswer : Answer
{
    public string Answer { get; set; }

    public virtual DiscursiveQuestion Question { get; set; }
}

public class ObjectiveAnswer : Answer
{
    public char Answer { get; set; }

    public virtual ObjectiveQuestion Question { get; set; }
}

public class VisualAnswer : Answer
{
    public byte[] Blob { get; set; } // Image answer

    public virtual VisualQuestion Question { get; set; }
}

对于我可能有的问题,而是一个public abstract class Bean { public int Id { get; set; } } 个对象和一个Question的客观问题。如果是这样,我们需要QuestionOption内的所有3个Answer对象,这对我来说听起来不对(需要识别问题类型,然后相应地访问其答案成员,如{ {1}}和Question强制转换)。对于解决方法,我决定使用TPH方法将问题分解为上面定义的3个对象,并拥有单独的答案成员。

所有内容似乎只适用于1个条件:所有流畅的API设置必须在is类的as内完成(我已覆盖它)。这是一个问题,因为我正在为每个实体对象填充所有配置并添加它们如下:

void OnModelCreating(DbModelBuilder modelBuilder)

这些是配置对象:

DbContext

为什么方法1工作而2不工作?

修改

我删除了配置继承,它“几乎”有效(见下文)。像这样:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Approach 1: This works
    //modelBuilder.Entity<Question>().Map<DiscursiveQuestion>(p => p.Requires("TP_QUESTION").HasValue("D")).ToTable("TB_QUESTION");
    //modelBuilder.Entity<Question>().Map<ObjectiveQuestion>(p => p.Requires("TP_QUESTION").HasValue("O")).ToTable("TB_QUESTION");
    //modelBuilder.Entity<Question>().Map<VisualQuestion>(p => p.Requires("TP_QUESTION").HasValue("V")).ToTable("TB_QUESTION");

    // Approach 2: This does not work: it complains that 2 of the 3 entities cant share the TB_QUESTION table because "they are not in the same type hierarchy 
    // or do not have a valid one to one foreign key relationship with matching primary keys between them" (???).
    modelBuilder.Configurations.Add(new VisualQuestionConfiguration());
    modelBuilder.Configurations.Add(new ObjectiveQuestionConfiguration());
    modelBuilder.Configurations.Add(new DiscursiveQuestionConfiguration());
}

并按照以下方式注册:

public abstract class QuestionConfiguration<T> : EntityTypeConfiguration<T> where T : Question
{
    public QuestionConfiguration()
    {
        Property(p => p.Statement).HasColumnName("STATEMENT");
    }
}

public class DiscursiveQuestionConfiguration : QuestionConfiguration<DiscursiveQuestion>
{
    public DiscursiveQuestionConfiguration()
    {
        Map(p => p.Requires("TP_QUESTION").HasValue("D")).ToTable("TB_QUESTION");
    }
}

public class VisualQuestionConfiguration : QuestionConfiguration<VisualQuestion>
{
    public VisualQuestionConfiguration()
    {
        Map(p => p.Requires("TP_QUESTION").HasValue("V")).ToTable("TB_QUESTION");
    }
}

public class ObjectiveQuestionConfiguration : QuestionConfiguration<ObjectiveQuestion>
{
    public ObjectiveQuestionConfiguration()
    {
        Map(p => p.Requires("TP_QUESTION").HasValue("O")).ToTable("TB_QUESTION");
    }
}

1 个答案:

答案 0 :(得分:1)

不同之处在于,第一种方法告诉EF将基本抽象类Question视为实体(modelBuilder.Entity<Question>()调用)而第二种方法不是。

您需要为Question创建并注册单独的配置。由于您将在那里配置所有公共属性,QuestionConfiguration<T>类是多余的。

这是第二种方法的正确实施。

配置:

public class QuestionConfiguration : EntityTypeConfiguration<Question>
{
    public QuestionConfiguration()
    {
        Property(p => p.Statement).HasColumnName("STATEMENT");
        ToTable("TB_QUESTION");
    }
}

public class DiscursiveQuestionConfiguration : EntityTypeConfiguration<DiscursiveQuestion>
{
    public DiscursiveQuestionConfiguration()
    {
        Map(p => p.Requires("TP_QUESTION").HasValue("D"));
    }
}

public class VisualQuestionConfiguration : EntityTypeConfiguration<VisualQuestion>
{
    public VisualQuestionConfiguration()
    {
        Map(p => p.Requires("TP_QUESTION").HasValue("V"));
    }
}

public class ObjectiveQuestionConfiguration : EntityTypeConfiguration<ObjectiveQuestion>
{
    public ObjectiveQuestionConfiguration()
    {
        Map(p => p.Requires("TP_QUESTION").HasValue("O"));
    }
}

注册:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new QuestionConfiguration());
    modelBuilder.Configurations.Add(new VisualQuestionConfiguration());
    modelBuilder.Configurations.Add(new ObjectiveQuestionConfiguration());
    modelBuilder.Configurations.Add(new DiscursiveQuestionConfiguration());
}
相关问题