【问题标题】:How to implement a recursive relationship with SQLite-Net Extentions如何使用 SQLite-Net Extensions 实现递归关系
【发布时间】:2014-05-26 08:39:31
【问题描述】:

我有一个表 Employee,它必须具有 递归关系 例如。

第 1 行: EmployeeId = 1EmployeeName = Albert,SupervisorId = NULL;

第 2 行EmployeeId = 2EmployeeName = Leonardo,SupervisorId = 1;

即EmployeeId(2) 是 EmployeeId(1) 的子代

我在 C# 中有以下代码,其中我使用 SQLite-Net Extentions 来实现递归关系:

public class Employee
{
    public Employee()
    {
        Subordinates = new List<Employee>();
    }

    [PrimaryKey]
    [MaxLength(36)]
    public string EmployeeGuid { get; set; }

    public string EmployeeName { get; set; }

    [OneToMany("SupervisorGuid")]
    public List<Employee> Subordinates { get; set; }

    [ManyToOne("EmployeeGuid")]
    public Employee Supervisor { get; set; }

    [ForeignKey(typeof(Employee))]
    [MaxLength(36)]
    public string SupervisorGuid { get; set; }
}

接下来我测试代码——我创建两个员工并将他们添加到表员工

第一个员工

Employee employee1 = new Employee
{
    EmployeeGuid = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
    EmployeeName = "Albert"
};

Insert(employee1);

第二名员工

Employee employee2 = new Employee
{
    EmployeeGuid = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
    EmployeeName = "Leonardo",
    SupervisorGuid = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
};

Insert(employee2);

但是当我打电话时

GetByGuid(string guid) 

guid 是第一个员工,我收到以下错误:

附加信息:“Project.Models.Employee”类型的对象无法转换为“System.Collections.Generic.List`1”类型

SQLite-Net 是否支持递归关系?请问有什么建议吗?

更新

GetByGuid() 的代码:

public T GetByGuid(string guid)
{
    return Database.GetWithChildren<T>(guid);
}

另外,当我添加 2nd 员工而不指定外键并进行调用时,它可以工作......

【问题讨论】:

  • 你能显示GetByGuid的代码还是这是SQLite-extensions的内置方法?

标签: c# recursion relationship sqlite-net sqlite-net-extensions


【解决方案1】:

在递归关系中,您必须手动指定反向关系,如下所示:

public class Employee
{
    [PrimaryKey]
    [MaxLength(36)]
    public string EmployeeGuid { get; set; }

    public string EmployeeName { get; set; }

    [OneToMany(inverseProperty: "Supervisor")]
    public List<Employee> Subordinates { get; set; }

    [ManyToOne(inverseProperty: "Subordinates")]
    public Employee Supervisor { get; set; }

    [ForeignKey(typeof(Employee))]
    [MaxLength(36)]
    public string SupervisorGuid { get; set; }
}

我已经对其进行了测试,并且可以按预期工作。但是,我创建了a new issue in bitbucket,因为我认为这种行为可以得到改善,因此这种情况将在不久的将来起作用:

public class Employee
{
    [PrimaryKey]
    [MaxLength(36)]
    public string EmployeeGuid { get; set; }

    public string EmployeeName { get; set; }

    [OneToMany]
    public List<Employee> Subordinates { get; set; }

    [ManyToOne]
    public Employee Supervisor { get; set; }

    [ForeignKey(typeof(Employee))]
    [MaxLength(36)]
    public string SupervisorGuid { get; set; }
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    似乎GetWithChildren 不返回T,而是List&lt;T&gt;,所以你需要这样做例如:

    public IEnumerable<T> GetByGuid(string guid)
    {
        return Database.GetWithChildren<T>(guid);
    }
    

    或者如果您只想要带有提供的Guid 的项目:

    public T GetByGuid(string guid)
    {
        return Database.GetWithChildren<T>(guid)
                   .FirstOrDefault(i => i.EmployeeGuid == guid);
    }
    

    但是GetWithChildren 可能是错误的方法。

    【讨论】:

    • GetWithChildren() 只能返回一个元素,但正如您所说,它正在返回一个集合 - 所以我会尝试找到解决方法
    • 如果是这种情况,您可以使用Database.GetWithChildren&lt;T&gt;(guid).FirstOrDefault()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 2023-01-19
    • 2015-12-11
    • 1970-01-01
    • 2019-09-15
    相关资源
    最近更新 更多