我试图导航到一个与另一个对象相关的对象。
盘点会议
和
地方
我对C#/SQLite/ORM很陌生,所以我想知道这是否就是它应该如何工作的,或者我需要使用不同的方法吗?
谢谢你的建议。
罗伯特
// 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);