【问题标题】:SQLiteNetExtensions multiple ManyToOne relations of same typeSQLiteNetExtensions 多个相同类型的多对一关系
【发布时间】:2019-02-27 08:28:23
【问题描述】:

我正在尝试 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 的帐户

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多