Multiple relationships of to same type throws IncorrectRelationshipException

Issue #23 resolved
Matt Benic created an issue

Using the following classes:

    [Table("ExerciseMultipleChoiceSentence")]
    public class ExerciseMultipleChoiceSentence
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        [ForeignKey(typeof(Sentence))]
        public int QuestionSentenceId { get; set; }
        [OneToOne]
        public Sentence QuestionSentence { get; set; }

        [ManyToMany(typeof(SentenceExerciseMultipleChoiceSentence))]
        public Sentence[] IncorrectAnswerSentences { get; set; }
    }

    [Table("Sentence")]
    public class Sentence
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        public string English { get; set; }

        [ManyToMany(typeof(SentenceExerciseMultipleChoiceSentence))]
        public ExerciseMultipleChoiceSentence[] ExerciseMultipleChoiceSentences { get; set; }

    [Table("SentenceExerciseMultipleChoiceSentence")]
    public class SentenceExerciseMultipleChoiceSentence
    {
        [ForeignKey(typeof(Sentence))]
        public int SentenceId { get; set; }

        [ForeignKey(typeof(ExerciseMultipleChoiceSentence))]
        public int ExerciseId { get; set; }
    }

Attempting to create and insert data throws an IncorrectRelationshipException when inserting a ExerciseMultipleChoiceSentence

Comments (3)

  1. Guillermo GutiƩrrez

    The problem is that the library tries to locate the inverse relationship for Sentence. ExerciseMultipleChoiceSentences and it will find a OneToOne relationship first, instead of the ManyToMany.

    This error will be fixed as soon as the Issue #17 is implemented, that will improve discovery of inverse relationships. Until then, you can simply specify the inverse relationship manually like this:

    In Sentence:

    [ManyToMany(typeof(SentenceExerciseMultipleChoiceSentence), inverseProperty: "IncorrectAnswerSentences"))]
    public ExerciseMultipleChoiceSentence[] ExerciseMultipleChoiceSentences { get; set; }
    

    And in ExerciseMultipleChoiceSentence, set the inverse property to "" to let the library know that it doesn't have any inverse property:

    [OneToOne(inverseProperty: "")]
    public Sentence QuestionSentence { get; set; }
    

    Hope it helps.

  2. Log in to comment