0

我是实体框架的新手,在尝试首先实现我的 ERD 代码时遇到了一些问题。情况如下:

ERD

一个产品有一组问题(QuestionGroup)。一个 QuestionGroup 有多个问题,可以属于多个 Questionaires。问卷基本上有一个问题组和一个问题顺序。questionorder 应该保持问题在该问卷中的位置。Questionaire 表是必需的,因为一个 QuestionGroup 可以有多个 questionorder,并且一个 questionorder 可以属于多个 questiongroup。

因为我正在尽力使这篇文章保持简洁,所以我不会发布我所有的课程,除非您要求查看它们。我制作的实体类如下所示:

public class Question
    {
        [Key]
        public int Id { get; set; }

        [MaxLength(2000)]
        [Required]
        public string Text { get; set; }
        public Answer Answer { get; set; }
        public QuestionType Type { get; set; }
        public ICollection<QuestionAnswerOption> Options { get; set; }
        public ICollection<QuestionOrder> Orders { get; set; }

        [ForeignKey("FollowupQuestion")]
        public int QuestionId { get; set; }
        public virtual Question FollowupQuestion { get; set; }
        [ForeignKey("Questiongroup")]
        public int QuestionGroupId { get; set; }
        public virtual QuestionGroup Questiongroup { get; set; }

    }

public class QuestionOrder
    {
        [Key]
        public int Id { get; set; }

        public int Position { get; set; }

        [ForeignKey("Question")]
        [Required]
        public int QuestionId { get; set; }
        public virtual Question Question { get; set; }
        [ForeignKey("Questionaire")]
        [Required]
        public int QuestionaireId { get; set; }
        public virtual Questionaire Questionaire { get; set; }

    }

  public class Product
    {
        [Key]
        public int Id { get; set; }

        [MaxLength(150)]
        [Required]
        [Index(IsUnique = true)]
        public string Name { get; set; }
        [MaxLength(500)]
        public string Summary { get; set; }
        [MaxLength(500)]
        public string Examples { get; set; }
        public virtual QuestionGroup QuestionGroup { get; set; }
    }

public class QuestionGroup
    {
        [Key]
        public int Id { get; set; }

        public ICollection<Question> Questions { get; set; }

        [ForeignKey("Product")]
        public int ProductId { get; set; }
        public virtual Product Product { get; set; }

    }

 public class Questionaire
    {
        [Key]
        public int Id { get; set; }

        public QuestionGroup Group { get; set; }

        public QuestionOrder Order { get; set; }
    }

我得到的错误如下所示:

QuestionGroup_Product_Source::多重性在关系“QuestionGroup_Product”中的角色“QuestionGroup_Product_Source”中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是“*”。

QuestionOrder_Questionaire_Source::多重性在关系“QuestionOrder_Questionaire”中的角色“QuestionOrder_Questionaire_Source”中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是“*”。

QuestionType_Question_Source::多重性在关系“QuestionType_Question”中的角色“QuestionType_Question_Source”中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是“*”。

我们在这里遇到的问题是:这里有没有人知道修复我的表之间关系的方法?也许我的 ERD 也需要一些改进,但我认为问题在于我的 Code First 实现缺少一些东西。

4

1 回答 1

0

这个设计对你来说怎么样;)

public class Customer
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CustomerId { get; set; }
    public string CompanyName { get; set; }

    public virtual ICollection<ContactInfo> ContactInfoes { get; set; }
    public virtual ICollection<ProductQuestionaireReport> Reports { get; set; }
}
public class ContactInfo
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int ContactInfoId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Telephone { get; set; }

    [ForeignKey("Customer")]
    public int CustomerId { get; set; }
    public virtual Customer Customer { get; set; }
}
public class ProductQuestionaireReport
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ProductQuestionaireReportId { get; set; }

    [ForeignKey("Product")]
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }

    public virtual QuestionaireResult Result { get; set; }

    [ForeignKey("Customer")]
    public int CustomerId { get; set; }
    public virtual Customer Customer { get; set; }

    public bool Haste { get; set; }
    public string Status { get; set; }
    public string Comment { get; set; }
}
public class QuestionaireResult
{
    [Key, ForeignKey("ProductQuestionaireReport")]
    public int ProductQuestionaireReportId { get; set; }

    [ForeignKey("Questionaire")]
    public int QuestionaireId { get; set; }
    public virtual Questionaire Questionaire { get; set; }

    public DateTime SurveyCreated { get; set; }
    public Double Costs { get; set; }
    public Double FunctionPoints { get; set; }

    public virtual ProductQuestionaireReport ProductQuestionaireReport { get; set; }
    public virtual ICollection<QuestionaireAnswer> Answers { get; set; }
}
public class Product
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string Summary { get; set; }
    public string Examples { get; set; }

    public virtual ICollection<Questionaire> Questionaires { get; set; }
    public virtual ICollection<ProductQuestionaireReport> Reports { get; set; }
}
public class QuestionaireAnswer
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int QuestionaireAnswerId { get; set; }

    [ForeignKey("QuestionaireResult")]
    public int QuestionaireResultId { get; set; }
    public virtual QuestionaireResult QuestionaireResult { get; set; }

    [ForeignKey("QuestionaireQuestion")]
    public int QuestionaireId { get; set; }
    public virtual QuestionaireQuestion QuestionaireQuestion { get; set; }

    [ForeignKey("Answer")]
    public int AnswerId { get; set; }
    public virtual QuestionOption Answer { get; set; }

    public string Text { get; set; }
    public virtual ICollection<string> Files { get; set; }
}
public class Questionaire
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int QuestionaireId { get; set; }
    [ForeignKey("Product")]
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }

    public virtual ICollection<QuestionaireQuestion> QuestionaireQuestions { get; set; }
    public virtual ICollection<QuestionaireResult> Results { get; set; }
}
public class Question
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int QuestionId { get; set; }

    [ForeignKey("ParentQuestion")]
    public int ParentQuestionId { get; set; }
    public virtual Question ParentQuestion { get; set; }

    public virtual QuestionType QuestionType { get; set; }
    public virtual ICollection<QuestionOption> Options { get; set; }
}
public class QuestionType
{
    [Key, ForeignKey("Question")]
    public int QuestionId { get; set; }

    public string Text { get; set; }
    public bool IsOpenQuestion { get; set; }

    public virtual Question Question { get; set; }
}
public class QuestionOption
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int QuestionAnswerOptionId { get; set; }

    [ForeignKey("Question")]
    public int QuestionId { get; set; }
    public string Text { get; set; }

    public virtual Question Question { get; set; }
}
public class QuestionaireQuestion
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int QuestionaireQuestionId { get; set; }

    [ForeignKey("Questionaire")]
    public int QuestionaireId { get; set; }
    public virtual Questionaire Questionaire { get; set; }

    [ForeignKey("Question")]
    public int QuestionId { get; set; }
    public int DisplayOrder { get; set; }

    public virtual Question Question { get; set; }
}

您还必须将其添加到OnModelCreating

modelBuilder.Entity<Customer>().HasMany(c => c.ContactInfoes)
    .WithRequired(i => i.Customer).HasForeignKey(i => i.CustomerId).WillCascadeOnDelete(false);

modelBuilder.Entity<Question>().HasMany(q => q.Options)
    .WithRequired(o => o.Question).HasForeignKey(o => o.QuestionId).WillCascadeOnDelete(false);

modelBuilder.Entity<QuestionaireResult>().HasMany(r => r.Answers)
    .WithRequired(a => a.QuestionaireResult).HasForeignKey(o => o.QuestionaireResultId).WillCascadeOnDelete(false);

检查并反馈我...

于 2015-10-09T14:49:12.690 回答