【发布时间】:2016-05-30 03:09:18
【问题描述】:
我目前正在通过 npgsql PostgreSQL 适配器在 ASP.NET Core 1.0 中使用 Linq to SQL。
我有以下 LINQ 查询,预计会返回 Device 类型的列表
var devices = DeviceConfigurationDbContext.UserGroupMembershipList
.AsNoTracking()
.Where(ugml => ugml.UserId == currentUser.Id)
.Select(ugml => ugml.UserGroup)
.SelectMany(ug => ug.UserGroupAccessList)
.Select(uga => uga.DeviceGroup)
.SelectMany(dg => dg.Devices);
此代码的目的是通过对UserGroupMembershipList 执行Where 来查找允许某个用户访问的所有设备,然后将其余表加入到Device 列表中到达。
实体之间的关系是:
UserGroupMembershipList -(多对一)-> UserGroup -(一对多)-> UserGroupAccessList -(多对一)-> DeviceGroup -(一对多)-> Device
UserGroupAccessList 是一个 ACL,它充当 UserGroup 和 DeviceGroup 之间的多对多连接表。
然后生成 SQL 查询:
SELECT "ugml"."Id", "ugml"."DeviceGroupId", "d"."DeviceGroupId", "d"."Name", "uga.DeviceGroup"."Id"
FROM "Device" AS "ugml"
INNER JOIN "UserGroup" AS "ugml.UserGroup" ON "ugml"."UserGroupId" = "ugml.UserGroup"."Id"
CROSS JOIN "UserGroupAccess" AS "uga"
INNER JOIN "DeviceGroup" AS "uga.DeviceGroup" ON "uga"."DeviceGroupId" = "uga.DeviceGroup"."Id"
CROSS JOIN "Device" AS "d"
WHERE ("ugml"."UserId" = @__currentUser_Id_0) AND ("ugml.UserGroup"."Id" = "ugml"."UserGroupId")
这又会产生错误
迭代结果时数据库中发生异常 询问。 Npgsql.NpgsqlException: 42703: ugml.UserGroupId 列没有 存在
这似乎是因为 SQL 查询出于某种原因执行 SELECT FROM "Device" AS ugml 而不是 SELECT FROM "UserGroupMembershipList" AS ugml。此外,因此 where 子句似乎不正确。
当涉及到 Linq 查询时,我做错了吗?有没有其他方法可以解决我正在尝试完成的事情,可以避免这个错误?
编辑:
我找到了一种解决方法,尽管它不太理想。
var devices = (await DeviceConfigurationDbContext.UserGroupMembershipList
.AsNoTracking()
.Where(ugml => ugml.UserId == currentUser.Id)
.Include(o => o.UserGroup)
.ThenInclude(o => o.UserGroupAccessList)
.ThenInclude(o => o.DeviceGroup)
.ThenInclude(o => o.Devices)
.ToListAsync())
.Select(ugml => ugml.UserGroup)
.SelectMany(ug => ug.UserGroupAccessList)
.Select(uga => uga.DeviceGroup)
.SelectMany(dg => dg.Devices);
这使得查询加入WHERE之后的表,然后将整个结果集作为List返回,标准Linq可以在内存中对其进行操作。这不太理想,因为我需要在之后进一步细化查询,与在数据库中执行所有操作相比,传输的数据要多得多。
【问题讨论】:
标签: c# linq postgresql entity-framework-core npgsql