簡體   English   中英

如何在C#類中為SQLite制作外鍵

[英]How to make foreign key in C# class for SQLite

如何在C#類中創建外鍵?

我有兩個單獨的類, CustomerSales

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

https://www.nuget.org/packages/SQLiteNetExtensions/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM