9

我是创建 Windows 应用商店应用程序的新手,它需要使用数据库。我已经选择了 sqlite 并且正在使用 sqlite-net 包。但是,我不确定如何在两个模型之间创建 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 回答