检索具有所有子元素(和子子元素)的对象

时间:2017-06-23 11:39:28

标签: c# sqlite xamarin sqlite-net sqlite-net-extensions

我想检索一个object,它本身有一些object类型的属性。这些子对象再次包含一些object作为属性。

如果我尝试从数据库中检索数据,我会在第一级获得子项的结果,但连续级别的数据为null。在下面的示例中,Windownull。从其他属性我按预期获得数据。如果我查看数据库,这种关系似乎是正确的。只有数据读数有误。

关系

One Way Relationship

汽车

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

    public string Model { get; set; }
    public string Color { get; set; }

    [ForeignKey(typeof(Door))]
    public int DoorId { get; set; }

    [OneToOne]
    public Door Door { get; set; }
}

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

    public string Side { get; set; }

    [ForeignKey(typeof(Window))]
    public int WindowId { get; set; }

    [OneToOne]
    public Window Window { get; set; }
}

窗口

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

    public bool Automatic { get; set; }
}

实施例

public MainPage()
{
    InitializeComponent();

    Window window = new Window();
    window.Automatic = true;

    Door door = new Door();
    door.Side = "left";
    door.Window = window;

    Car car = new Car();
    car.Color = "red";
    car.Model = "MX5";
    car.Door = door;

    this.car = car;

}

protected override async void OnAppearing()
{
    base.OnAppearing();

    await App.Database.StoreCarAsync(this.car);
    Car existingCar = await App.Database.GetCarAsync(this.car.Id);
    System.Diagnostics.Debug.WriteLine("finished");
}

数据库

public class Database
{
    private readonly SQLiteAsyncConnection database;

    public Database(string databasePath)
    {
        this.database = new SQLiteAsyncConnection(databasePath);
        this.database.CreateTableAsync<Car>().Wait();
        this.database.CreateTableAsync<Door>().Wait();
        this.database.CreateTableAsync<Window>().Wait();
    }

    public async Task<Car> GetCarAsync(int carId)
    {
        var queryResult = await this.database.Table<Car>().Where(c => c.Id == carId).CountAsync();
        if (queryResult > 0)
        {
            return await this.database.GetWithChildrenAsync<Car>(carId);
        }
        else
        {
            return null;
        }
    }

    public async Task<Door> GetDoorAsync(int doorId)
    {
        var queryResult = await this.database.Table<Door>().Where(d => d.Id == doorId).CountAsync();
        if (queryResult > 0)
        {
            return await this.database.GetWithChildrenAsync<Door>(doorId);
        }
        else
        {
            return null;
        }
    }

    public async Task<Window> GetWindowAsync(int windowId)
    {
        var queryResult = await this.database.Table<Window>().Where(w => w.Id == windowId).CountAsync();
        if (queryResult > 0)
        {
            return await this.database.GetWithChildrenAsync<Window>(windowId);
        }
        else
        {
            return null;
        }
    }

    public async Task StoreCarAsync(Car car)
    {
        await this.StoreDoorAsync(car.Door);

        var foundItem = await this.GetCarAsync(car.Id);
        if (foundItem != null)
        {
            await this.database.UpdateWithChildrenAsync(car);
        }
        else
        {
            await this.database.InsertWithChildrenAsync(car);
        }
    }

    public async Task StoreDoorAsync(Door door)
    {
        await this.StoreWindowAsync(door.Window);

        var foundItem = await this.GetDoorAsync(door.Id);
        if (foundItem != null)
        {
            await this.database.UpdateWithChildrenAsync(door);
        }
        else
        {
            await this.database.InsertWithChildrenAsync(door);
        }
    }

    public async Task<int> StoreWindowAsync(Window window)
    {
        var foundItem = await this.GetWindowAsync(window.Id);
        if (foundItem != null)
        {
            return await this.database.UpdateAsync(window);
        }
        else
        {
            return await this.database.InsertAsync(window);
        }
    }
}

这不可能或我在这里做错了吗?

PS:我正在使用最新的SQLiteNetExtensions.Async v2.0.0-alpha2 NuGet包。

1 个答案:

答案 0 :(得分:2)

您应该使用cascaded read来阅读完整的关系层次结构。首先标记您的实体关系以允许级联读取,例如:

public class Car
{
    //snip

    [OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public Door Door { get; set; }
}

然后,当您从数据库中读取时,将recursive参数设置为true。例如:

return await this.database.GetWithChildrenAsync<Door>(doorId, recursive: true);