1

我为一个类似 Google Forms 的项目建模。下面的实体非常简单明了(我猜),如下所示。

问题类型:

// 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; }
}

答案类型:

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用于客观问题。如果是这样,我们将需要Answer里面的所有 3 个对象Question,这对我来说听起来不对(需要识别问题类型,然后相应地访问其答案成员,如isand ascasts)。作为一种解决方法,我决定使用TPH方法将问题分成上面定义的 3 个对象,并拥有单独的 Answer 成员。

一切似乎都只适用于 1 个条件:所有流畅的 API 设置必须在void OnModelCreating(DbModelBuilder modelBuilder)类中完成DbContext(我已经覆盖了它)。这是一个问题,因为我正在为每个实体对象分离所有配置并像这样添加它们:

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 有效,而方法 2 无效?

编辑:

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

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

        // Configures the TPH
        Map<VisualQuestion>(p => p.Requires("TYPE").HasValue("Visual").HasMaxLength(10));
        Map<ObjectiveQuestion>(p => p.Requires("TYPE").HasValue("Objective").HasMaxLength(10));
        Map<DiscursiveQuestion>(p => p.Requires("TYPE").HasValue("Discursive").HasMaxLength(10));

        ToTable("TB_QUESTION");
    }
}

public class DiscursiveQuestionConfiguration : Configuration<DiscursiveQuestion>
{
    public DiscursiveQuestionConfiguration()
    {
    }
}

public class VisualQuestionConfiguration : Configuration<VisualQuestion>
{
    public VisualQuestionConfiguration()
    {
    }
}

public class ObjectiveQuestionConfiguration : Configuration<ObjectiveQuestion>
{
    public ObjectiveQuestionConfiguration()
    {
    }
}

public class ObjectiveQuestionOptionConfiguration : Configuration<ObjectiveQuestionOption>
{
    public ObjectiveQuestionOptionConfiguration()
    {
        HasRequired(p => p.Question).WithMany(p => p.Options).HasForeignKey(p => p.ObjectiveQuestionId);

        Property(p => p.ObjectiveQuestionId).HasColumnName("ID_OBJECTIVE_QUESTION");
        Property(p => p.Statement).HasColumnName("STATEMENT"); // <--- This doesnt get mapped! :(

        ToTable("TB_OBJECTIVE_QUESTION_OPTION");
    }
}

并像这样注册它们:

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 work too, however ObjectiveQuestionOption* does not inherit the statement column
    modelBuilder.Configurations.Add(new QuestionConfiguration());
    modelBuilder.Configurations.Add(new QuestionOptionConfiguration());
}
4

1 回答 1

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());
}
于 2017-10-02T19:54:01.793 回答