C# 如何使用SQLite网络扩展实现递归关系

C# 如何使用SQLite网络扩展实现递归关系,c#,recursion,relationship,sqlite-net,sqlite-net-extensions,C#,Recursion,Relationship,Sqlite Net,Sqlite Net Extensions,我有一个表Employee,它必须具有递归关系,例如 第1行:员工ID=1,员工姓名=Albert,主管ID=NULL 第2行:员工ID=2,员工姓名=莱昂纳多,主管ID=1 i、 e.EmployeeId(2)是EmployeeId(1)的子项 我在C#中有以下代码,用于实现递归关系: public class Employee { public Employee() { Subordinates = new List<Employee>();

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

第1行:员工ID=1,员工姓名=Albert,主管ID=NULL

第2行:员工ID=2,员工姓名=莱昂纳多,主管ID=1

i、 e.EmployeeId(2)是EmployeeId(1)的子项

我在C#中有以下代码,用于实现递归关系:

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; }
}
第二雇员

但是当我打电话的时候

GetByGuid(string guid) 
guid为第一名员工时,我得到以下错误:

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

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

更新

GetByGuid()的代码:

公共T GetByGuid(字符串guid) { 返回数据库.GetWithChildren(guid); }
另外,当我在没有指定外键的情况下添加第二名员工并执行调用时,它也会工作…

似乎
GetWithChildren
不会返回
T
,而是
列表
,因此您需要执行以下操作,例如:

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

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

似乎
GetWithChildren
不会返回
T
,而是
List
,因此您需要执行以下操作:

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

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

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

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; }
}
公共类员工
{
[主密钥]
[MaxLength(36)]
公共字符串EmployeeGuid{get;set;}
公共字符串EmployeeName{get;set;}
[OneToMany(反向属性:“主管”)]
公共列表下属{get;set;}
[多通(反向属性:“下属”)]
公共雇员主管{get;set;}
[外键(类型(员工))]
[MaxLength(36)]
公共字符串SupervisorGuid{get;set;}
}
我已经对它进行了测试,效果如预期。但是,我之所以创建此案例,是因为我认为此行为可以改进,所以此案例将在不久的将来生效:

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; }
}
公共类员工
{
[主密钥]
[MaxLength(36)]
公共字符串EmployeeGuid{get;set;}
公共字符串EmployeeName{get;set;}
[一家公司]
公共列表下属{get;set;}
[许多人]
公共雇员主管{get;set;}
[外键(类型(员工))]
[MaxLength(36)]
公共字符串SupervisorGuid{get;set;}
}

希望有帮助。

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

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; }
}
公共类员工
{
[主密钥]
[MaxLength(36)]
公共字符串EmployeeGuid{get;set;}
公共字符串EmployeeName{get;set;}
[OneToMany(反向属性:“主管”)]
公共列表下属{get;set;}
[多通(反向属性:“下属”)]
公共雇员主管{get;set;}
[外键(类型(员工))]
[MaxLength(36)]
公共字符串SupervisorGuid{get;set;}
}
我已经对它进行了测试,效果如预期。但是,我之所以创建此案例,是因为我认为此行为可以改进,所以此案例将在不久的将来生效:

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; }
}
公共类员工
{
[主密钥]
[MaxLength(36)]
公共字符串EmployeeGuid{get;set;}
公共字符串EmployeeName{get;set;}
[一家公司]
公共列表下属{get;set;}
[许多人]
公共雇员主管{get;set;}
[外键(类型(员工))]
[MaxLength(36)]
公共字符串SupervisorGuid{get;set;}
}

希望有帮助。

您能显示
GetByGuid
的代码吗?或者这是SQLite扩展的内置方法吗?您能显示
GetByGuid
的代码吗?或者这是SQLite扩展的内置方法吗?GetWithChildren()只能返回一个元素,但正如您所说,它正在返回一个集合-因此,如果您可以使用
数据库.GetWithChildren(guid).FirstOrDefault()
.GetWithChildren()只能返回一个元素,但正如您所说,它正在返回一个集合-因此,如果您可以使用
Database.GetWithChildren(guid).FirstOrDefault()
,我将尝试找到解决方法。
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; }
}