繁体   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