[英]How to make foreign key in C# class for SQLite
如何在C#類中創建外鍵?
我有兩個單獨的類, Customer
和Sales
。
class Customer
{
[PrimaryKey,AutoIncrement]
public int ID { get; set; }
public string name { get; set; }
public string address { get; set; }
[MaxLength(11)]
public int phone { get; set; }
public double Account_payable { get; set; }
}
class Sales
{
[PrimaryKey,AutoIncrement]
public int OrderID { get; set;}
// here I want to add a foreign key to the Customer table
}
請使用https://bitbucket.org/twincoders/sqlite-net-extensions
這里有很多例子:
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<Valuation> Valuations { get; set; }
}
public class Valuation
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Stock))] // Specify the foreign key
public int StockId { get; set; }
public DateTime Time { get; set; }
public decimal Price { get; set; }
[ManyToOne] // Many to one relationship with Stock
public Stock Stock { get; set; }
}
Sqlite.net沒有外鍵支持,但是您可以使用Sqlite.net-extensions
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.