与EF7(Core)中的同一张表的多个关系 [英] Multiple relationships to the same table in EF7(Core)

查看:186
本文介绍了与EF7(Core)中的同一张表的多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的模型

public class Question
{
    public string Id { get; set; } = Guid.NewGuid().ToString();

    public Answer Answer { get; set; }
    public List<Variant> Variants { get; set; }

    public string CorrectVariantId { get; set; }
    public Variant CorrectVariant { get; set; }
}

public class Variant
{
    public string Id { get; set; } = Guid.NewGuid().ToString();

    public string QuestionId { get; set; }
    public Question Question { get; set; }
}

// mapping

modelBuilder.Entity<Question>()
    .HasOne(q => q.CorrectVariant)
    .WithOne(v => v.Question)
    .HasForeignKey<Question>(q => q.CorrectVariantId);

modelBuilder.Entity<Variant>()
    .HasOne(v => v.Question)
    .WithMany(a => a.Variants)
    .OnDelete(DeleteBehavior.Cascade);

在我从EF RC1升级到RTM之前,这一切工作得很好.但是现在它抛出: System.InvalidOperationException: Cannot create a relationship between 'Question.Variants' and 'Variant.Question', because there already is a relationship between 'Question.CorrectVariant' and 'Variant.Question'. Navigation properties can only participate in a single relationship.

Which worked perfectly until I upgraded from EF RC1 to RTM. But now it throws: System.InvalidOperationException: Cannot create a relationship between 'Question.Variants' and 'Variant.Question', because there already is a relationship between 'Question.CorrectVariant' and 'Variant.Question'. Navigation properties can only participate in a single relationship.

是否有解决此问题的方法,而不仅仅是从Question模型中删除Variants属性?

Is there any workaround for this problem without just deleting Variants property from the Question model?

推荐答案

以防有人遇到这个问题. 这是更优雅的解决方案

In case someone will run into this question. Here is more elegant solution

public class Question
{
    public Guid Id { get; private set; }
    public IReadOnlyList<Variant> Variants { get; private set; }
    public Guid CorrectVariantId { get; private set; }
    public Guid? AnsweredVariantId { get; private set; }    
    public bool IsAnswerCorrect => CorrectVariantId == AnsweredVariantId;
    public bool IsAnswered => AnsweredVariantId != null;
}

public class Variant
{
    public Guid Id { get; private set; }
    public Guid QuestionId { get; private set; }
    public string HiddenUserLogin { get; private set; }
    public User HiddenUser { get; private set; }
}

// mapping
mb.Entity<Question>()
    .HasMany(q => q.Variants)
    .WithOne()
    .HasForeignKey(nameof(Variant.QuestionId))
    .IsRequired()
    .OnDelete(DeleteBehavior.Cascade);

mb.Entity<Question>()
    .HasOne(typeof(Variant))
    .WithOne()
    .HasForeignKey<Question>(nameof(Question.AnsweredVariantId))
    .IsRequired(false) 
    .OnDelete(DeleteBehavior.Restrict);

// EF creates Unique Index for nullable fields
mb.Entity<Question>()
    .HasIndex(q => q.AnsweredVariantId)
    .IsUnique(false);

// create index instead of FK hence the cyclic dependency between Question and Variant
mb.Entity<Question>()
    .HasIndex(q => q.CorrectVariantId)
    .IsUnique();

这篇关于与EF7(Core)中的同一张表的多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆