C# SqlSugarClient分析一点
时间:2021-07-01 10:21:17
帮助过:119人阅读
class UserManager : DbContext<User>
{}
UserManager userdb =
new UserManager();
//创建对象
var user = userdb.GetFirst(u => u.UserName == userName && u.Password ==
pwd);
//继承,可以使用父类方法GetFirst()
public class DbContext<T>
where T :
class,
new()
{
public SqlSugarClient Db;
//用来处理事务多表查询和复杂的操作
public DbContext()
{
Db =
new SqlSugarClient(
new ConnectionConfig()
{ … });
}
public virtual T GetFirst(Expression<Func<T,
bool>>
whereExpression)
{
return Db.Queryable<T>
().First(whereExpression);
}
}
SqlSugarClient Db对象下面的方法
public class SqlSugarClient : ISqlSugarClient, IDisposable, Itenant
{
public SqlSugarProvider Context =>
GetContext();
//public ISugarQueryable<T> Queryable<T>();
public ISugarQueryable<T> Queryable<T>
()
{
return Context.Queryable<T>();
//返回的对象的类是继承于ISugarQueryable<T>接口
//所以可以继续点first方法 Db.Queryable<T>().First(whereExpression);
}
private SqlSugarProvider GetContext()
{
if (IsSameThreadAndShard())
{
return SameThreadAndShard();
}
if (IsNoSameThreadAndShard())
{
return NoSameThreadAndShard();
}
if (IsSynchronization())
{
return Synchronization();
}
return NoSameThread();
}
}
//接口 ; 继承并实现first方法
public interface ISugarQueryable<T>
{
T First(Expression<Func<T,
bool>>
expression);
}
// \.nuget\packages\sqlsugarcore\5.0.0.1\lib\netstandard2.0\SqlSugar.dll
cehsimodelManager abc = new cehsimodelManager();
abc.GetFirst(u => u.btnname == "admin" && u.btnid == "123465"); //c#
//sql
exec sp_executesql N‘SELECT * FROM (SELECT [btnname],[btnid],ROW_NUMBER() OVER(ORDER BY GETDATE() ) AS RowIndex
FROM [cehsimodel] WHERE (( [btnname] = @btnname0 ) AND ( [btnid] = @btnid1 ))) T WHERE RowIndex BETWEEN 1 AND 1‘,N‘@btnname0 nvarchar(4000),@btnid1 nvarchar(4000)‘,@btnname0=N‘admin‘,@btnid1=N‘123465‘
--[cehsimodel] 类的名称要和表名称一致
public class cehsimodelManager : DbContext<cehsimodel>
{}
//类
public class cehsimodel
{
public string btnname { get; set; }
public string btnid { get; set; }
}
C# SqlSugarClient分析一点
标签:amp private ESS sha getc 返回 iss use pass