SQLiteNetExtensions多个相同类型的ManyToOne关系

时间:2019-02-27 08:28:24

标签: sqlite xamarin.forms sqlite-net-extensions

我正在尝试使用xamarin应用程序...我需要sqlite数据库...该数据库包含3个表...用户,帐户和交易记录...帐户表似乎:

[Table(nameof(Account))]
class Account
{
    [PrimaryKey,AutoIncrement]
    public int Id { get; set; }

    public string Name { get; set; }

    [ForeignKey(typeof(User))]
    public int UserId { get; set; }

    public double Balance { get; set; }

    [ManyToOne]
    public User User { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<Transaction> Transactions { get; set; }
}

我认为直到这里都没有问题... 但在交易表格中有两列FirstAccountIdTargetAccountId,所以我喜欢:

[Table(nameof(Transaction))]
class Transaction
{
    [PrimaryKey,AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Account))]
    public int TargetAccountId { set; get; }

    [ForeignKey(typeof(Account))]
    public int FirstAccountId { get; set; }

    public DateTime DateOfTransaction { get; set; }

    [ManyToOne()]
    public Account Account1 { get; set; }

    [ManyToOne()]
    public Account Account2 { get; set; }`
}

我如何制作Account1FirstAccountId的帐户,而Account2TargetAccountId的帐户

1 个答案:

答案 0 :(得分:0)

根据ManyToOnesee here)的源代码,它以string foreignKey作为参数。我没有在文档中找到任何有关此内容的信息,但我假设您可以使用此参数显式指定外键,尽管我在文档中没有找到任何相关信息

[ManyToOne(nameof(FirstAccountId))]
public Account Account1 { get; set; }

[ManyToOne(nameof(TargetAccountId))]
public Account Account2 { get; set; }