小编Ham*_*eli的帖子

EFCore 中的 CTE(公用表表达式)

我的数据库中有一个表,用于存储位置的层次结构。它有 3 列(Id, Name, ParentId)。我需要根据条件加载一些行以及它们的所有父行直到根。如果是在 ADO 中,我会使用以下语句。

with Temp
as
(
    select * from Locations where Name like '%filter%'

    union all

    select Locations.* 
    from Temp join Locations on Temp.ParentId = Locations.Id
)
select * from Temp
Run Code Online (Sandbox Code Playgroud)

我正在使用 EFCore,并通过一些搜索发现实体框架如何与递归层次结构一起工作?Include() 似乎不适用于它 如何使用实体框架进行递归加载?还有一堆其他的,他们都老了。

我发现的所有解决方案要么对层次结构的深度进行硬编码(使用Include),要么在 C# 中进行递归。所有这些,我的问题是最好的解决方案是什么?

我可以使用FromSqlRaw (类似下面的代码),但是我不喜欢在 C# 中手动编写查询。

    var locations = DataContext.Locations
    .FromSqlRaw("MyQuery").ToList();
Run Code Online (Sandbox Code Playgroud)

我在用EFCore 3.1.7

sql-server recursion hierarchy sql-cte entity-framework-core

10
推荐指数
1
解决办法
5044
查看次数

打字稿:从通用接口中省略属性

我正在尝试创建一个省略给定类型的属性的接口。为此,我使用Omitwhich 结果在 Type 所以它的定义是错误的。但是,如果它不是通用接口,它就可以完美运行。

考虑以下示例。

interface IBaseType {
  prop1: number;
  prop2: string;
  match: boolean;
}

interface OmitMatchNoGeneric extends Omit<IBaseType, "match"> {}

interface OmitMatch<T extends { match: any }> extends Omit<T, "match"> {}

function test(genericArg: OmitMatch<IBaseType>, nonGenericArg: OmitMatchNoGeneric) {
  nonGenericArg.prop1 = 5; // the properties are suggested
  genericArg.prop1 = 5; // intelliSense fails to list the properties
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,VSCode 的智能感知显示了非泛型参数的属性列表,但它没有为泛型参数显示。泛型参数被视为 any 类型的对象。

我主要关心的是,如果我不应该使用,Omit我还能使用什么?如果我想用类型而不是接口来实现它,我该怎么做?

extends interface typescript typescript-generics typescript-typings

4
推荐指数
1
解决办法
2299
查看次数