如何在SQLite的C#类中创建外键

时间:2017-04-27 13:31:49

标签: c# 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
} 

2 个答案:

答案 0 :(得分:1)

请使用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; }
}

答案 1 :(得分:0)

Sqlite.net没有外键支持,但您可以使用Sqlite.net-extensions

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