shuxinqin / Chloe

A lightweight and high-performance Object/Relational Mapping(ORM) library for .NET --C#
https://github.com/shuxinqin/Chloe/wiki
MIT License
1.52k stars 455 forks source link

过滤器对Count、CountAsync、LongCount、LongCountAsync无效 #344

Closed stackdenglang closed 1 year ago

stackdenglang commented 1 year ago
<TargetFramework>net6.0</TargetFramework>
<PackageReference Include="Chloe.Extension" Version="5.2.0" />
<PackageReference Include="Chloe.MySql" Version="5.2.0" />
using Chloe.Annotations;
using Chloe.Entity;
using Chloe.Infrastructure;
using Chloe.MySql;
using MySql.Data.MySqlClient;
using System.Data;

DbConfiguration.UseTypeBuilders(typeof(PatientInfoMap));

MySqlContext db = new MySqlContext(new MysqlDbConnectionFactory());
//db.HasQueryFilter<PatientInfo>(x => x.IsDeleted == false);

var ableCount = db.Query<PatientInfo>().Count();
var allCount = db.Query<PatientInfo>().IgnoreAllFilters().Count();

long id = 2;
Console.WriteLine($"ALL:{allCount},AbleCount:{ableCount}");

var count = db.Query<PatientInfo>().Where(x => x.Id == id).Count();
var countAsync = db.Query<PatientInfo>().Where(x => x.Id == 2).CountAsync().Result;
var longCount = db.Query<PatientInfo>().Where(x => x.Id == 2).LongCount();
var longCountAsync = db.Query<PatientInfo>().Where(x => x.Id == 2).LongCountAsync().Result;
var listCount = db.Query<PatientInfo>().Where(x => x.Id == 2).ToList().Count;

Console.WriteLine($"Count:{count},ListCount:{listCount},LongCount:{longCount},longCountAsync:{longCountAsync}");
Console.ReadLine();

class MysqlDbConnectionFactory : IDbConnectionFactory
{
    public IDbConnection CreateConnection()
    {
        return new MySqlConnection("server=127.0.0.1;port=3306;database=pmvte;uid=root;pwd=root");
    }
}

//[Table("vte_PatientInfo")]
public class PatientInfo
{
    public long Id { get; set; }
    public bool IsDeleted { get; set; }
}

public class PatientInfoMap : EntityTypeBuilder<PatientInfo>
{
    public PatientInfoMap()
    {
        MapTo("vte_PatientInfo");

        HasQueryFilter(x => x.IsDeleted == false);
    }
}

输出: SELECT COUNT(1) AS C FROM vte_PatientInfo AS vte_PatientInfo SELECT COUNT(1) AS C FROM vte_PatientInfo AS vte_PatientInfo ALL:20391,AbleCount:20391 SELECT COUNT(1) AS C FROM vte_PatientInfo AS vte_PatientInfo WHERE vte_PatientInfo.Id = ?P_0 SELECT COUNT(1) AS C FROM vte_PatientInfo AS vte_PatientInfo WHERE vte_PatientInfo.Id = 2 SELECT COUNT(1) AS C FROM vte_PatientInfo AS vte_PatientInfo WHERE vte_PatientInfo.Id = 2 SELECT COUNT(1) AS C FROM vte_PatientInfo AS vte_PatientInfo WHERE vte_PatientInfo.Id = 2 SELECT vte_PatientInfo.Id AS Id,vte_PatientInfo.IsDeleted AS IsDeleted FROM vte_PatientInfo AS vte_PatientInfo WHERE (vte_PatientInfo.Id = 2 AND vte_PatientInfo.IsDeleted = 0) Count:1,ListCount:0,LongCount:1,longCountAsync:1

shuxinqin commented 1 year ago

v5.3.0 已修复