2017-09-22 7 views
1
abgerufen wird

Ich versuche, zu einem Objekt zu navigieren, das sich auf ein anderes Objekt bezieht.Navigieren zu SQLite-Objekt in Bezug auf ein anderes Objekt, das aus der Datenbank

In dieser Situation gibt es 2 Klassen; StockTakeSession und Position. Diese 2 haben eine Eins-zu-Eins-Beziehung. Wenn zwei Objekte von jeder Klasse erstellt werden, setze ich die Beziehung wie im folgenden Code. Ich kann so ohne Probleme durchgehen wie ich es mag. Aber Wenn ich eine StockTakeSession aus der Datenbank abrufe, hat nur LocationId einen Wert und Location selbst ist null. Um dies zu lösen, habe ich die erweiterte Methode get für StockTakeSession.Location erstellt.

Ich bin ziemlich neu mit C#/SQLite/ORM, also habe ich mich gefragt, ob das genauso ist, wie es funktionieren soll oder muss ich einen anderen Ansatz verwenden?

Danke für jeden Vorschlag.

Robert


Dies sind die zwei Klassen:

// 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; } 

} 

Wie ich die Beziehung zu speichern zwischen Objekten:

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

}; 

db.Insert(newSession, true); 

Antwort

2

In SQLite-net-Erweiterungen, Beziehungen nicht auf Anfrage geladen. Sie müssen sie von der Datenbank holen, wenn Sie sie brauchen.

Ändern Sie Ihre Implementierung für Location zu einem einfachen Getter/Setter.

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

Dann laden die Beziehungen Sitzung, wenn Sie es brauchen:

db.GetChildren(session); 

Zum Beispiel:

db.GetWithChildren<StockTakeSession>(stockId); 

Wird das StockTakeSession Objekt mit dieser ID holen und auch Beziehungen laden und stellen Sie die inverse Beziehungen.

+0

Danke! Das funktioniert gut! – Robert

Verwandte Themen