SQLite.NET 多对多扩展,未创建加入 table 条记录

SQLite.NET Extensions Many To Many, no join table records created

我刚开始使用 SQLite.NET 和扩展。

尽我最大的能力,我已经按照我找到的指南进行操作,但是在多对多连接表中没有创建任何记录,我不知道为什么。

我有一个解决方案 NuGet 对 SQLiteNetExtensions 项目的依赖。

我有以下表格:

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

    [MaxLength(50)]
    public string FirstName { get; set; }

    [MaxLength(50)]
    public string Surname { get; set; }

    [ManyToMany(typeof(Participant))]
    public List<Journey> Journeys { get; set; }
}

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

    [ManyToMany(typeof(Participant))]
    public List<Contact> Contacts { get; set; }

    [ManyToMany(typeof(Waypoint))]
    public List<Location> Locations { get; set; }
}

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

    [MaxLength(50)]
    public string Name { get; set; }

    public double Latitude { get; set; }

    public double Longitude { get; set; }

    [ManyToMany(typeof(Waypoint))]
    public List<Journey> Journeys{ get; set; }
}

public class Participant
{
    [ForeignKey(typeof(Contact))]
    public int ContactId { get; set; }

    [ForeignKey(typeof(Journey))]
    public int JourneyId { get; set; }
}

public class Waypoint
{
    [ForeignKey(typeof(Location))]
    public int LocationId { get; set; }

    [ForeignKey(typeof(Journey))]
    public int JourneyId { get; set; }
}

当我构建数据库时,我使用以下测试代码:

string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "xandroid.db3");

var db = new SQLiteConnection(platform, dbPath);

db.DropTable<Location>();
db.DropTable<Waypoint>();
db.DropTable<Contact>();
db.DropTable<Participant>();
db.DropTable<Journey>();

db.CreateTable<Location>();
db.CreateTable<Waypoint>();
db.CreateTable<Contact>();
db.CreateTable<Participant>();
db.CreateTable<Journey>();

Location home = new Location { Name = "Home", Latitude=22.22, Longitude=22.22 };
Location perth = new Location { Name = "Perth", Latitude = 4444.4444, Longitude = 4444.4444 };
db.InsertAll(new List<Location> { home, perth });

Contact beans = new Contact { FirstName = "Beans", Surname = "Connor" };
Contact winston = new Contact { FirstName = "Winston", Surname = "Connor" };
db.InsertAll(new List<Contact> { beans, winston });

Journey travelToPerth = new Journey { Locations = new List<Location> { perth }, Contacts = new List<Contact> { beans, winston }};
Journey returnHome = new Journey { Locations = new List<Location> { home }, Contacts = new List<Contact> { beans, winston}};
db.InsertAll(new List<Journey> { travelToPerth, returnHome } );

当我访问数据时,我使用以下代码:

var waypoints = db.Table<Waypoint>();
Console.Out.WriteLine(waypoints.Count() + " recorded waypoints");

var participants = db.Table<Participant>();
Console.Out.WriteLine(participants.Count() + " recorded participants");

var journeys = db.Table<Journey>();
Console.Out.WriteLine(journeys.Count() + " recorded journeys");

其输出为:

0 recorded waypoints
0 recorded participants
2 recorded journeys

您正在使用普通 sqlite.net 方法插入对象,这些方法对您的关系一无所知。要保存和加载关系,您必须使用 SQLite-Net 扩展方法。大多数 sqlite-net 方法还有一个 WithChildren 替代方法来保存关系:

import SQLiteNetExtensions.Extensions;

db.InsertAllWithChildren(new List<Journey> { travelToPerth, returnHome } );

将插入两个元素并将所需的记录插入到 ParticipantWaypoint 表以保存关系。

注意:这仍然需要 LocationContact 元素已经插入到数据库中。要以递归方式插入对象,请查看 SQLite-Net Extensions documentation.

级联操作 部分