EF.Core 使用Linq的Contact联合查询问题

文章讲述了在使用EFCore5进行.NETCoreWebAPI开发时遇到的多表联合查询问题,由于查询结果集中ReplyContent列的存储类型不一致导致错误。解决方案是通过使用Convert.ToString()将不同长度的字符串转换为同一类型,从而成功执行联合查询。
摘要由CSDN通过智能技术生成

在.net Core 5 WebAPI 项目中应用 EF Core 5 实体框架,使用Linq的Contact联合进行多表查询。

定义两个子查询语句,查询结果 select 返回的对象结构类型都是一致的。

var query1 = from auth in Context.Authentications
              where auth.Status == 1
              select new ComplainInfo
              {
                  UserId = auth.UserId,
                  Reason = auth.Reason,
                  ReplyContent = "",
                  ReplyTime = (DateTime?)null
              };
                             
var query2 = from complaint in Context.DatingComplaints
              where complaint.Status = 1
              select new ComplainInfo
              {
                  UserId = complaint.UserId,
                  Reason = complaint.Reason,
                  ReplyContent = complaint.ReplyContent,
                  ReplyTime = complaint.ReplyTime
              };

//联合两个子查询
var query = query1.Contact(query2);

var items = await query.ToListAsync();

Linq查询结果集封装对象类:

public class ComplainInfo
{
    public long UserId { get; set; }
    public string Reason { get; set; }
    public string ReplyContent { get; set; }
    public DateTime? ReplyTime { get; set; }
}

DatingComplaint 实体类映射(Fluent API):

public class DatingComplaint
{
    public long Id { get; set; }
    public long UserId { get; set; }
    public string Reason { get; set; }
    public string ReplyContent { get; set; }

    public DateTime? ReplyTime { get; set; }
}

public class DatingComplaintEntity : IEntityTypeConfiguration<DatingComplaint>
{
    public void Configure(EntityTypeBuilder<DatingComplaint> builder)
    {
        builder
            .ToTable("DatingComplaint")
            .HasKey(o => o.Id);

        builder
            .Property(o => o.Id)
            .UseHiLo("DatingComplaintId")
            .IsRequired();

        builder
            .Property(o => o.UserId)
            .IsRequired();

       
        builder
            .Property(o => o.Reason)
            .HasMaxLength(1000)
            .IsRequired();

        builder
           .Property(o => o.ReplyContent)
           .HasMaxLength(1000)
           .IsRequired(false);

        builder
            .Property(o => o.ReplyTime)
            .IsRequired(false);   
    }
}

但在执行到 ToListAsync() 代码行查询返回结果时,抛出以下错误:

Unable to translate set operation when matching columns on both sides have different store types

经检查发现,query1 子查询中的 Replycontent 与 query2 子查询中的 ReplyContent 匹配列具有不同的存储类型,导致无法进行 union 集合查询操作。

从 DatingComplaint 实体类定义中可以看到,ReplyContent 属性同样使用 string 类型来定义, 与 Linq 查询结果集封装对象 ComplainInfo 中的 ReplyContent 属性都是使用 string 类型定义。若是从对象属性类型来看,很难判断出具有不同的存储类型的问题所在。

进行一步分析,既然是类型(string)一致,会不会字符串类型的长度不一致,导致进行 union 集合查询时 EF 认为是不同的存储类型?

实体类定义的 ReplyContent 属性映射的是可变室长的字符串类型(即:nvarchar(1000)),而类对象中的string类型 EF.core 会解析成最大长度的字符串类型(即: nvarchar(max))进行查询。

此时执行Linq查询时,就会产生匹配到列具有不同的存储类型的异常。

解决方案:

既然找到了是匹配列具有不同的存储类型的原因,将子查询语句中的列统一转换成相同类型再进行查询。

query1 查询语句中的 ReplyContent 非实体映射返回的属性值,EF.Core 解析生成列类型为 nvarchar(max)。可以将 query2 查询语句中 ReplyContent 实体映射返回的属性值类型(nvarchar(1000)),转换成 nvarchar(max)。

可以使用 Convert.ToString() 方法进行转换处理。

var query2 = from complaint in Context.DatingComplaints
              where complaint.Status = 1
              select new ComplainInfo
              {
                  UserId = complainUser.Id,
                  Reason = complaint.Reason,
                  ReplyContent = Convert.ToString(complaint.ReplyContent),
                  ReplyTime = complaint.ReplyTime
              };

执行Linq查询语句生成的SQL语句可以看到 对 ReplyContent 列进行了 nvarchar(max) 类型的转换,如下:

SELECT [a].[UserId], [a].[Reason], N'' AS [ReplyContent], NULL AS [ReplyTime]
FROM [Authentication] AS [a]
WHERE ([a].[Status] = 1) 
UNION ALL
SELECT [d].[UserId], [d].[Reason], CONVERT(nvarchar(max), [d].[ReplyContent]) AS [ReplyContent], [d].[ReplyTime]
FROM [DatingComplaint] AS [d]
WHERE [d].[Status] = 1

注:若两个实体对象使用相同属性类型(string)但字符串长度不一致时,也可以使用类型转换方式来处理。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值