小编Kas*_*hov的帖子

使用自定义表达式扩展 EF Core 'where' 子句

我有一堆实体,它们具有定义的活动期,如“StartDate”和“EndDate”字段。大多数时候我需要查询他们根据一些自定义值检查他们的活动期。代码几乎如下所示:

public static Expression<Func<T, bool>> IsPeriodActive<T>(DateTime checkPeriodStart, DateTime checkPeriodEnd, Func<T, DateTime> entityPeriodStart, Func<T, DateTime> entityPeriodEnd) =>
    entity =>
        (checkPeriodEnd >= entityPeriodStart(entity) && checkPeriodEnd <= entityPeriodEnd(entity))
        || (checkPeriodStart >= entityPeriodStart(entity) && checkPeriodEnd <= entityPeriodEnd(entity))
        || (entityPeriodStart(entity) >= checkPeriodStart && entityPeriodStart(entity) <= checkPeriodEnd)
        || (entityPeriodEnd(entity) >= checkPeriodStart && entityPeriodEnd(entity) <= checkPeriodEnd)
        || (entityPeriodStart(entity) >= checkPeriodStart && entityPeriodStart(entity) <= checkPeriodEnd);
Run Code Online (Sandbox Code Playgroud)

问题是 Func.Invoke() 不能翻译成 SQL,这很明显。我如何扩展 EF Core 为任何实体类型添加这种“where”条件?我不能使用过滤器,因为有时我需要查询原始数据或只进行一次周期检查(不是两者),而且某些实体的这些字段名称不同。

c# linq entity-framework-core

5
推荐指数
1
解决办法
4970
查看次数

如何在 EF Core Code First 中自定义迁移生成?

我的DbContext. 当从它继承时,我需要生成一个额外的“SQL”迁移操作来为它创建一个特定的触发器。它通过检查重叠范围来确保表结构是一致的。由于在 SQL Server 中没有重叠的索引或检查约束,我必须使用触发器(在检查约束中使用函数会导致与迁移相同的问题以及 SQL 中的混乱函数“命名空间”)。

由于我在OnModelCreating考虑更改生成的迁移期间没有找到任何创建触发器的方法。但是怎么做呢?

尝试使用SqlServerMigrationsSqlGeneratorand SqlServerMigrationsAnnotationProvider,但顾名思义,它们仅在生成 SQL 命令期间的最后阶段使用。这使得它们在使用迁移时有点“隐藏”。在需要时难以定制并在之后进行维护。

考虑使用CSharpMigrationOperationGenerator这似乎非常适合我的需求。但是有一个问题 - 我无法访问这个类。也不是命名空间。

根据来源,此类驻留在Microsoft.EntityFrameworkCore.Migrations.Design命名空间中并且是公共的。为了访问它Microsoft.EntityFrameworkCore.Design,必须安装一个包。

但它不起作用。

我在这里缺少什么?如何访问和继承这个类?或者也许有更好和正确的方法在特定表的迁移期间自动创建触发器?

c# sql-server entity-framework-core asp.net-core entity-framework-migrations

4
推荐指数
3
解决办法
2773
查看次数

Azure使用C#使用多个方法运行单个HTTP触发器

这个功能目前是否可行?"方法"是指多个HTTP动词,如"get","post","put"等.

在使用控制器的Web API中,我们可以通过在控制器类中分配不同方法调用的属性来实现.

在天蓝色的功能中有这样的东西吗?

c# http azure azure-functions

3
推荐指数
1
解决办法
2256
查看次数

如何在一个查询中获取具有角色的用户?

我已经设置了我的身份模型,就像这里描述的那样: https: //learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model ?view=aspnetcore-2.2#add-user-and -角色导航属性

所以我的每个User类都有一个Roles的集合,通过UserRole“包装器”。所有实体关系都已设置。

当我查询我的用户时,我得到每个用户的所有角色(此处使用延迟加载,但“包含”没有区别):

var users = _userManager.Users
    .AsNoTracking()
    .ToList();
Run Code Online (Sandbox Code Playgroud)

但是,当检查 EF Core 创建的日志时,我发现每个用户都有另一个查询来获取角色:

[Parameters=[@_outer_Id='d550f61b-ed3d-4d90-8e7b-31552de50d3b' (Size = 450)], CommandType='"Text"', CommandTimeout='30']
SELECT [r].[RoleId] AS [Id], [r.Role].[Name], [r.Role].[DisplayName]
FROM [AspNetUserRoles] AS [r]
INNER JOIN [AspNetRoles] AS [r.Role] ON [r].[RoleId] = [r.Role].[Id]
WHERE ([r].[Discriminator] = N'UserRole') AND (@_outer_Id = [r].[UserId])
Run Code Online (Sandbox Code Playgroud)

这会针对数据库中的每个用户 ID 重复进行。

如何仅使用一个查询即可获得结果?

以防万一,我的模型:

public class User : IdentityUser
{
    public virtual ICollection<UserRole> UserRoles { get; set; }
}

public class UserRole : IdentityUserRole<string>
{
    public virtual User User …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core asp.net-core asp.net-core-identity

3
推荐指数
1
解决办法
4995
查看次数

为什么 Regex.Replace 给我最后一组奇怪的结果

一个简单的例子:

Regex.Replace("12345678910999999999", @"(\d{3})(\d{3})(\d{3})(\d{2})", "$1-$2-$3 $4")
Run Code Online (Sandbox Code Playgroud)

这输出到:

123-456-789 10999999999
Run Code Online (Sandbox Code Playgroud)

但为什么?我已经专门设置了我需要的组索引。并且该组索引包含确切值(在调试器中检查)。

这是一个小提琴:https : //dotnetfiddle.net/dkAPx3

c# regex

2
推荐指数
1
解决办法
53
查看次数

EF Core 不断抱怨索引不存在

我在运行时收到来自 EF Core 的一堆日志抱怨,如下所示:

[10:56:14 DBG] The index {'InvoiceId'} was not created on entity type 'CompleteCase' as the properties are already covered by the index {'InvoiceId', 'Code'}.
[10:56:14 DBG] The index {'ReportPeriodId'} was not created on entity type 'FixedBill' as the properties are already covered by the index {'ReportPeriodId', 'MedCompanyTypeId', 'FixedBillTypeId'}.
[10:56:14 DBG] The index {'CasePeriodId'} was not created on entity type 'Invoice' as the properties are already covered by the index {'CasePeriodId', 'ReportPeriodId', 'MedCompanyTypeId'}.
[10:56:14 DBG] The index {'InvoiceId'} …
Run Code Online (Sandbox Code Playgroud)

sql-server foreign-keys relational-database entity-framework-core entity-framework-migrations

2
推荐指数
1
解决办法
1530
查看次数