SqlSugar进行初始化,数据库连接配置,代码可以去SqlSugar官网 查看源码,下面给出一个简单的案例,SqlSugar的具体介绍官网非常清晰明了,这里之说干货。
1.Sugar连接配置部分:
public class SqlSugarInit
{
#region 创建数据库连接对象
public SqlSugarClient Db { get; private set; }
#endregion
#region 初始化时读取连接字符串信息
public static string ConnectionString = CustomConfig.Read(); //替换为你的连接字符串
#endregion
public SqlSugarInit()
{
ConnectionConfig dB = new ConnectionConfig()
{
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
ConnectionString = ConnectionString
};
Db = new SqlSugarClient(dB, db =>
{
db.Aop.OnLogExecuting = (sql, pars) =>
{
//获取原生SQL推荐 5.1.4.63 性能OK
Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
//获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用
//Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))
};
//注意多租户 有几个设置几个
//db.GetConnection(i).Aop
ConfigureExternalServices configureExternalServices = new ConfigureExternalServices()
{
EntityService = (property, column) =>
{
var attributes = property.GetCustomAttributes(true);//get all attributes
if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey
{
column.IsPrimarykey = true; //有哪些特性可以看 1.2 特性明细
}
},
EntityNameService = (type, entity) =>
{
var attributes = type.GetCustomAttributes(true);
if (attributes.Any(it => it is TableAttribute))
{
var attr = (attributes.First(it => it is TableAttribute) as TableAttribute);
entity.DbTableName = attr.Name;
}
}
};
});
}
#region 创建数据库
public void CreateDatabase()
{
//建立数据库
if (Db.DbMaintenance.CreateDatabase())
{
Console.WriteLine("数据块建立成功!");
// 查找所有命名空间
// 解析 C# 代码
string targetNamespace = "Common.SugarModels";
// 获取当前程序集中的所有类型
Type[] allTypes = Assembly.GetExecutingAssembly().GetTypes();
// 筛选出指定命名空间下的类型
Type[] typesInNamespaces = allTypes.Where(t => t.Namespace == targetNamespace & t.IsClass).ToArray();
Db.CodeFirst.InitTables(typesInNamespaces);
}
}
#endregion
}
2.实体
public partial class Factory
{
public Factory()
{
}
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[CRUDId("Id")]
[DisplayName("序号")]
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
/// Desc:网址
/// Default:
/// Nullable:False
/// </summary>
[DisplayName("获取地址")]
public string RequestUrlStr { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[DisplayName("请求地址")]
public string UploadUrlStr { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[CRUDValue("Name")]
[DisplayName("工厂名称")]
public string Name { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[DisplayName("是否启用")]
public bool IsEnable { get; set; }
}
///<summary>
///
///</summary>
public partial class ProductionLine
{
public ProductionLine()
{
}
private int id;
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[CRUDIdAttribute("Id")]
[DisplayName("序号")]
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get { return id; } set { id = value; } }
public int FactoryId { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[DisplayName("工厂")]
[CRUDTable(typeof(Factory))]
[Navigate(NavigateType.OneToOne, nameof(FactoryId))]
public Factory Factory { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[DisplayName("产线名称")]
[CRUDValueAttribute("Name")]
public string Name { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[DisplayName("是否启用")]
public bool IsEnable { get; set; }
}
3.通过导航查询的代码,本次查询采用的一对一查询。具体代码可以查考这里
//通过导航进行查询
__Init__.ProductionLines = sqlSugarInit.Db.Queryable<ProductionLine>()//From StudentA x
.Includes(x => x.Factory)
.Where(p => p.IsEnable == true)
.ToList();
查询结果
标签:set,False,get,Nullable,介绍,DisplayName,导航,public,SqlSugar From: https://blog.csdn.net/qq_44774906/article/details/144153695