gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:29:42 26 4
gpt4 key购买 nike

如何在 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; }
}

关于c# - 如何在 SQLite 的 C# 类中创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43659395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com