如何在linq2Db中加载子集合?

我在sql中创建了以下表格。

create table Department
(
    Id uniqueidentifier primary key not null,Name nvarchar(255),Isactive bit
)

create table Employee
(
    Id uniqueidentifier primary key not null,Age int,Address nvarchar(255),Email varchar(max),Dob datetime,DeptId uniqueidentifier,Isactive bit,Foreign key (DeptId) references Department(Id)
)

在雇员表中,“ DeptId”是外键。 我已经编写了代码来提取其部门的所有员工。

这里的问题是我的部门员工空缺
我记得在使用EF内核时,我做了这样的事情:

 var employees = db.Employees.Include(e => e.Department);

上面的代码返回具有部门的员工,但是当使用linq2Db时,如何编写代码以获取具有部门的员工?

这是我的代码:
Employee.cs

    public class Employee
    {
        [PrimaryKey]
        public Guid Id { get; set; }
        public string Name { get; set; }
        public int? Age { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public DateTime? Dob { get; set; }
        public Guid? DeptId { get; set; }
        public bool? Isactive { get; set; }

        public virtual Department Department { get; set; }
    }

Department.cs

    public class Department
    {
        public Department()
        {
            this.Employees = new HashSet<Employee>();
        }

        [PrimaryKey]
        public System.Guid Id { get; set; }
        public string Name { get; set; }
        public bool? Isactive { get; set; }

        [Association(ThisKey = nameof(Department.Id),OtherKey = nameof(Employee.DeptId))]
        public virtual ICollection<Employee> Employees { get; set; }
    }

SampleDbContext.cs

    public class SampleDbContext : DataConnection
    {
        public SampleDbContext(LinqToDbConnectionOptions<SampleDbContext> options)
            : base(options)
        {

        }

        public ITable<Employee> Employees => GetTable<Employee>();
        public ITable<Department> Departments => GetTable<Department>();
    }

EmployeeController.cs

    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        private readonly SampleDbContext _sampleDbContext;

        public EmployeeController(SampleDbContext sampleDbContext)
        {
            _sampleDbContext = sampleDbContext;
        }

        // GET: api/<EmployeeController>
        [HttpGet]
        public async Task<Employee[]> Get()
        {
            return await _sampleDbContext.Employees.ToArrayAsync();
        }

    }

有人可以帮我吗?
谢谢!!

fleia 回答:如何在linq2Db中加载子集合?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1383836.html

大家都在问