1
我是使用SQLite.NET和扩展的新手。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
感谢吉列尔莫,添加使用声明解决了我的问题。如果您能够提供帮助,我想我可能会遇到次要问题。当我尝试坚持旅程项目(联系人和地点已被插入)时,我在“\”附近出现了一个有点不起眼的错误,其中\“:syntax error”;这听起来对我来说就像我的课程在扩展尝试使用它们时创建了破损的SQL,这意味着我可能犯了一个错误。我正在使用递归:对于旅程的最终InsertAllWithChildren和表中的所有关系都设置CascadeOperations来读取任何想法? – Syntax
如果元素已经插入,则不需要递归标志。尝试将主键添加到中间表。如果您认为它是库中的错误,请在https://bitbucket.org/twincoders/sqlite-net-extensions中创建一个错误。 – redent84