Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未创建SQLite外键,也未创建OneToMany列_Sqlite_Xamarin_Foreign Keys_Sqlite Net - Fatal编程技术网

未创建SQLite外键,也未创建OneToMany列

未创建SQLite外键,也未创建OneToMany列,sqlite,xamarin,foreign-keys,sqlite-net,Sqlite,Xamarin,Foreign Keys,Sqlite Net,我正在尝试创建SQLite DB,它有两个表,其中第二个表有外键。尽管使用了SQLite Net Extensions的示例代码,但我仍然无法实现这一点。每次运行代码时,都不会创建列 我正在使用sqlite net pcl(1.3.1)和sqlite net扩展 public class Bus { [PrimaryKey, AutoIncrement] public int Id { get; set; } public string PlateNumber { ge

我正在尝试创建SQLite DB,它有两个表,其中第二个表有外键。尽管使用了SQLite Net Extensions的示例代码,但我仍然无法实现这一点。每次运行代码时,都不会创建列

我正在使用sqlite net pcl(1.3.1)和sqlite net扩展

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

    public string PlateNumber { get; set; }

    [OneToMany]
    public List<Person> Passengers { get; set; }
}

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

    public string Name { get; set; }

    [ForeignKey(typeof(Bus))]
    public int BusId { get; set; }
}


        var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var library = Path.Combine(documents, "..", "Library");
        var filename = Path.Combine(library, "database.db3");
        var db = new SQLiteConnection(filename);

        db.CreateTable<User>();
        db.CreateTable<Purchase>();

        db.CreateTable<Bus>();
        db.CreateTable<Person>();

我认为您误解了sqlite扩展的功能。它不会在Bus表中为乘客创建列。(列表的列类型是什么…?)它将允许您从数据库检索总线,并让扩展自动使用适当链接的人员对象填充乘客属性,但前提是您使用BusId属性将乘客与总线关联。我明白了,现在它更有意义了。使用这种方法,我试图测试它,但它并没有像您描述的那样工作。我使用了上面的类和更新的代码。当我查看乘客时,它总是空的。
        db.DeleteAll<Bus>();
        db.DeleteAll<Person>();

        db.CreateTable<Bus>();
        db.CreateTable<Person>();

        db.Insert(new Bus { PlateNumber = "446s-sef5", Id = 1 });
        db.Insert(new Person { Name = "Petr" , BusId = 1});
        db.Insert(new Person { Name = "George" , BusId = 1});

        var busses = db.Table<Bus>();
        foreach (var bus in busses)
        {

        }