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
输出: SELECT COUNT(1) AS
C
FROMvte_PatientInfo
ASvte_PatientInfo
SELECT COUNT(1) ASC
FROMvte_PatientInfo
ASvte_PatientInfo
ALL:20391,AbleCount:20391 SELECT COUNT(1) ASC
FROMvte_PatientInfo
ASvte_PatientInfo
WHEREvte_PatientInfo
.Id
= ?P_0 SELECT COUNT(1) ASC
FROMvte_PatientInfo
ASvte_PatientInfo
WHEREvte_PatientInfo
.Id
= 2 SELECT COUNT(1) ASC
FROMvte_PatientInfo
ASvte_PatientInfo
WHEREvte_PatientInfo
.Id
= 2 SELECT COUNT(1) ASC
FROMvte_PatientInfo
ASvte_PatientInfo
WHEREvte_PatientInfo
.Id
= 2 SELECTvte_PatientInfo
.Id
ASId
,vte_PatientInfo
.IsDeleted
ASIsDeleted
FROMvte_PatientInfo
ASvte_PatientInfo
WHERE (vte_PatientInfo
.Id
= 2 ANDvte_PatientInfo
.IsDeleted
= 0) Count:1,ListCount:0,LongCount:1,longCountAsync:1