我有一个用例,其中我将查询传递给函数,然后进行一些计算。查询是根据我通过的过滤器形成的。下面是示例代码
var totalCount = await query.CountAsync();
var limitExceeded = limit.HasValue && totalCount > limit.Value;
var pagedResults = new List<R>();
// Don't execute the query if the limit has been exceeded
if (!limitExceeded)
{
//Do some work here
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是有一个名为“filename”的过滤器,当我通过该过滤器时,生成的底层查询如下所示
DECLARE @__fileName_1 nvarchar(1024) = N'%cmder.zip%';
SELECT [d].[Id], [d].[CreatedByUserId], [d].[DateCreated], [d].[DateModified], [d].[DatePurged], [d].[Deleted], [d].[DocumentKey], [d].[DocumentStatusId], [d].[DocumentTypeId], [d].[FileDesc], [d].[FileExt], [d].[FileLength], [d].[FileLengthTypeId], [d].[FileName], [d].[FileSize], [d].[FullPath], [d].[Hidden], [d].[ModifiedByUserId], [d].[PurgedByUserId], [d].[RepositoryId], [d].[SHA1], [d].[TransactionId], [d].[UploadedDate], [c].[Id], [c].[CaseId], [c].[DateModified], [c].[Deleted], [c].[DocumentId], [c].[ModifiedByUserId], [c].[PublishToId], [c].[TransactionId], [c0].[Id], [c0].[CaseCoordinatorId], [c0].[CaseName], [c0].[CaseNo], …Run Code Online (Sandbox Code Playgroud) 我有 sql server management studio (SSMS) v18.9.1 和 Visual Studio 2022 v17.1.1。
我正在使用 EF 迁移 (dotnet 6),对 pocos 进行更改后,我从 Visual Studio 中的包管理器控制台窗口运行add-migration,然后运行update-database 。
如果我尝试通过右键单击\Tables\Refresh 来刷新 SSMS,则会出现SSMS Tables (expanding...)消息。该表最终会出现,但这可能需要长达 10 分钟的时间。
我的同事在他的笔记本电脑上也遇到了同样的问题。
有什么想法如何修复吗?
我开发了一个简单的应用程序,类似于聊天,其中每条消息都可能包含文本和文件。实体之间的关系如下:
消息组 -> 每个消息组都有一个消息集合 -> 每条消息都有一个属性“FileCollection” -> “文件集合”有 4 个集合:图像、视频、音频、文件。它们在数据库中都有相同的关系。为了在此处显示此逻辑,我的查询是获取所有消息组及其实体:
var messageGroups = await _db.MessageGroups
.Where(mg => mg.UserId == id)
.Include(m => m.Messages).ThenInclude(mes => mes.FileCollection.Images)
.Include(m => m.Messages).ThenInclude(mes => mes.FileCollection.Video)
.Include(m => m.Messages).ThenInclude(mes => mes.FileCollection.Audio)
.Include(m => m.Messages).ThenInclude(mes => mes.FileCollection.Files)
.ToListAsync();
Run Code Online (Sandbox Code Playgroud)
问题是每种类型的文件(图像、音频等)在 Db(EF Core 中的属性)中都有一个“数据”列,其中包含其 blob 数据。我想从查询中排除所有 blob,因为从数据库加载所有用户文件的查询变得非常繁重。像这样的东西(但排除方法不存在):
.Include(m => m.Messages).ThenInclude(mes => mes.FileCollection.Video).exclude(video => video.Data);
Run Code Online (Sandbox Code Playgroud)
有没有办法在查询结束时使用显式加载?或者也许有像 [JsonIgnore] 这样的属性,它从 Json 序列化中排除类属性?或者有其他方法吗?
如果有帮助:ImageFile、AudioFile 等继承自 File 超类:
public class File
{
[Column("id")]
public int Id { get; set; }
[Column("content_type")]
public string ContentType …Run Code Online (Sandbox Code Playgroud) 我有以下查询:
SELECT c.customer_id
FROM order o
JOIN customer c USING (customer_id)
WHERE c.time >= '2021-01-01 10:00:00' AND c.time < '2021-01-01 11:00:00'
AND (0 IN (22) OR o.product_id IN (22))
LIMIT 1
Run Code Online (Sandbox Code Playgroud)
只要至少有一个匹配,就会返回一行,但是如果没有匹配,则不会有任何行。
如果没有匹配,是否可以返回一行空值?
我想从构造函数启动.NET 6 中发布的PeriodicTimer,并且在这个计时器中我想使用异步方法更新数据,我该如何处理它?
我已将 .NET Core 3.1 应用程序更新到 .NET 6,其中一个步骤是将 EntityFramwork Core 更新到版本 6.0.2。
不幸的是,我的许多单元测试现在都失败了,但有以下例外:
System.TypeLoadException:无法从程序集“Microsoft.EntityFrameworkCore,版本=6.0.2.0”加载类型“Microsoft.EntityFrameworkCore.Query.Internal.IAsyncQueryProvider”,
我不太确定为什么会发生这种情况。我已经阅读了迁移指南(https://learn.microsoft.com/en-us/aspnet/core/migration/31-to-60?view=aspnetcore-6.0&tabs=visual-studio),但还没有见过这样的事情。
以下是因此异常而失败的单元测试的一个示例:
[TestMethod]
public async Task IsPrivatKundeAsync_WithMatchingCriteria_ReturnsTrue()
{
var data = new[] {
new MyEntity { Partnernr = "1", Geburtstag = new DateTime(2000, 1, 1), Plz = "8000" },
new MyEntity { Partnernr = "2", Geburtstag = new DateTime(2001, 2, 2), Plz = "8001" },
new MyEntity { Partnernr = "3", Geburtstag = new DateTime(2002, 3, 3), Plz = "8002" },
};
var builder = …Run Code Online (Sandbox Code Playgroud) 我怀着极大的热情在 EF Core 7 中发现了 ExecuteDeleteAsync 和 ExecuteUpdateAsync。它们有助于使我的代码变得更简单、更快。无需使用自制程序批量删除或更新1-2个字段。无论如何,我遇到过应该在运行时选择要更新的数据库的确切表和字段的情况。
我可以使用数据库表:
public static IQueryable<object> Set(this DbContext context, Type entity) =>
context.ClrQuery(context.ClrType(entity));
Run Code Online (Sandbox Code Playgroud)
我有制作表达式来过滤行的方法:
public static IQueryable Where(this IQueryable source, string equalProperty, object value, [NotNull] Type EntityType)
{
PropertyInfo? property = EntityType.GetProperty(equalProperty);
if (property == null)
throw new NotImplementedException($"Type {EntityType.Name} does not contain property {equalProperty}");
ParameterExpression parameter = Expression.Parameter(EntityType, "r");
MemberExpression member = Expression.MakeMemberAccess(parameter, property);
LambdaExpression whereExpression = Expression.Lambda(Expression.Equal(member, Expression.Constant(value, property.PropertyType)), parameter);
MethodCallExpression resultExpression = WhereCall(source, whereExpression);
return source.Provider.CreateQuery(resultExpression);
}
Run Code Online (Sandbox Code Playgroud)
所以我可以找到要更新的行
IQueryable Source = …Run Code Online (Sandbox Code Playgroud) linq expression-trees entity-framework-core c#-11.0 ef-core-7.0
c# ×3
.net-6.0 ×2
linq ×2
sql-server ×2
.net ×1
c#-11.0 ×1
ef-core-7.0 ×1
null ×1
postgresql ×1
row ×1
sql ×1
ssms ×1
timer ×1