【问题标题】:Navigate to SQLite object related to an other object, fetched from the database导航到与其他对象相关的 SQLite 对象,从数据库中获取
【发布时间】:2017-09-22 08:19:00
【问题描述】:

我正在尝试导航到与另一个对象相关的对象。

在这种情况下,有 2 个类; StockTakeSession位置。这两个具有一对一的关系。当每个类创建 2 个对象时,我将关系设置为下面的代码。我可以随意浏览它,没有任何问题。 但是当我从数据库中获取 StockTakeSession 时,只有 LocationId 有值,而 Location 本身为空。为了解决这个问题,我为 StockTakeSession.Location 创建了扩展的 get 方法。

我对 C#/SQLite/ORM 很陌生,所以我想知道这是否就是它的工作原理,还是我需要使用不同的方法?

感谢您的任何建议。

罗伯特


这是 2 个类:

// These are the Stock Take Sessions
public class StockTakeSession : DomainModels
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public DateTime DateTime { get; set; }

    // Navigation properties
    // One to many relationship with StockItems
    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<StockItem> StockItems { get; set; }


    // Specify the foreign key to Location
    [ForeignKey(typeof(Location))]
    public int LocationId { get; set; }

    // One to one relationship with Location

    private Location location;

    [OneToOne]
    public Location Location
    {


        get
        {
            if (location == null)
            {
                DataBase db = new DataBase();

                Location locationTemp = db.SelectLocation(LocationId);

                return locationTemp;
            }
            else
                return location;

        }

        set
        {
            location = value;
        }
    }
}

// These are the Locations where the Stock Take Sessions are done
public class Location : DomainModels, IComparable<Location>
{
    [JsonProperty("id"), PrimaryKey]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Street { get; set; }
    public int Number { get; set; }
    public string Postcode { get; set; }
    public string City { get; set; }
    public bool Completed { get; set; }

    [Ignore]
    public string Label
    {
        get
        {
            return Name + " - (" + Postcode + ")";
        }
    }

    public int CompareTo(Location other)
    {
        return Name.CompareTo(other.Name);
    }

    // Navigation property
    // One to many relationship with StockItems
    [OneToMany(CascadeOperations = CascadeOperation.All), Ignore]
    public List<StockItem> StockItems { get; set; }

    // Specify the foreign key to StockTakeSession
    [ForeignKey(typeof(StockTakeSession))]
    public int StockTakeSessionId { get; set; }

    // One to one relationship with StockTakeSession
    [OneToOne]
    public StockTakeSession StockTakeSession { get; set; }

}

我如何存储对象之间的关系:

StockTakeSession newSession = new StockTakeSession
{
    LocationId = selectedLocation.Id,
    Location = selectedLocation,
    DateTime = DateTime.Now

};

db.Insert(newSession, true);

【问题讨论】:

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


    【解决方案1】:

    在 sqlite-net-extensions 中,关系不是按需加载的。您必须在需要时从数据库中获取它们。

    Location 的实现更改为简单的 getter/setter。

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

    然后,在需要时加载会话关系:

    db.GetChildren(session);
    

    例如:

    db.GetWithChildren<StockTakeSession>(stockId);
    

    将获取具有该 ID 的 StockTakeSession 对象,并加载关系并设置反向关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      相关资源
      最近更新 更多