9

Windows ストア アプリを作成するのは初めてで、データベースを使用する必要があります。私は sqlite に落ち着き、sqlite-net パッケージを使用しています。ただし、2 つのモデル間で m2m 関係を作成する方法がわかりません。

class ModelA
{
     [PrimaryKey, AutoIncrement]
     public int Id { get; set; }
     public string name { get; set; }
     <relationship to ModelB>
} 


class ModelB
{
     [PrimaryKey, AutoIncrement]
     public int Id { get; set; }
     public string name { get; set; }
}

リストを使用する必要がありますか? またはバイト[]?プロパティが に制約されることをどのように保証できますModelBか?

4

1 に答える 1

14

sqlite-net-extensionsを使用できる場合があります。これにはManyToMany、ニーズに最適な属性があります。これは彼らのウェブサイトからの使用例です。

public class Student
{
    [PrimaryKey, AutoIncrement]
    public int StudentId { get; set; }

    public string Name { get; set; }
    public int Age { get; set; }

    [ManyToMany(typeof(StudentsGroups))]
    public List<Group> Groups { get; set; }

    public int TutorId { get; set; }
    [ManyToOne("TutorId")] // Foreign key may be specified in the relationship
    public Teacher Tutor { get; set; }
}

public class Group
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string GroupName { get; set; }

    [ForeignKey(typeof(Teacher))]
    public int TeacherId { get; set; }

    [ManyToOne]
    public Teacher Teacher { get; set; }

    [ManyToMany(typeof(StudentsGroups))]
    public List<Student> Students { get; set; } 
}

public class StudentGroups
{
    [ForeignKey(typeof(Student))]
    public int StudentId { get; set; }

    [ForeignKey(typeof(Group))]
    public int GroupId { get; set; }
}
于 2014-04-17T05:10:48.827 に答える