实体配置
1.实体使用自带特性
对于CURD来说,只需要配置主键和自增列就行;类的名称和数据库表名不同时,也可设置
1.1 主键自增
[SugarTable("dbstudent")]//当和数据库名称不一样可以设置表别名 指定表明
public class Student
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增
public int Id { get; set; }
public int? SchoolId { get; set; }
[SugarColumn(ColumnName = "StudentName")]//数据库与实体不一样设置列名
public string Name { get; set; }
}
//创建表语句
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Student));
1.2 多个主键
public class Student
{
[SugarColumn(IsPrimaryKey = true)] //设置主键
public Guid Pk1{ get; set; }
[SugarColumn(IsPrimaryKey = true)] //设置主键
public Guid Pk2{ get; set; }
public string Name { get; set; }
}
1.3 无主键
public class Student
{
public Guid Id{ get; set; }
public string Name { get; set; }
}
2.实体使用自定义特性
2.1 创建特性的类
/// <summary>
/// 自定义类(表)特性
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public class CustomeAttributeClass : Attribute
{
//有参构造
public CustomeAttributeClass(string str)
{
testClass = str + "Test";
}
public string testClass { get; set; }
}
/// <summary>
/// 自定义属性(列)特性
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class CustomeAttributeColumn : Attribute
{
public CustomeAttributeColumn()
{
}
private string testColumn;
public string TestColumn
{
get { return testColumn; }
set { testColumn = value; }
}
}
2.2 创建带自定义特性的实体类
[CustomeAttributeClass("At")]
public class TestModel
{
public int Id { get; set; }
[CustomeAttributeColumn(TestColumn = "Ac")]
public string Name { get; set; }
public int Age { get; set; }
}
2.3在创建SqlSugarClient对象中添加代码
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
ConnectionString = "server = 127.0.0.1; Database = sugarlearn; Uid = root; Pwd = root; AllowLoadLocalInfile = true;",
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
//自定义特性
ConfigureExternalServices = new ConfigureExternalServices()
{
//属性(列)的处理
EntityService = (property, column) =>
{
var attributes = property.GetCustomAttributes(true);//get all attributes
if (attributes.Any(it => it is CustomeAttributeColumn))// by attribute set primarykey
{
column.DbColumnName = (attributes.First(it => it is CustomeAttributeColumn) as CustomeAttributeColumn).TestColumn;
}
},
//类(表)的处理
EntityNameService = (type, entity) =>
{
var attributes = type.GetCustomAttributes(true);
if (attributes.Any(it => it is CustomeAttributeClass))
{
entity.DbTableName = (attributes.First(it => it is CustomeAttributeClass) as CustomeAttributeClass).testClass;
}
}
}
});
2.4添加创建表的代码,查看效果
static void Main(string[] args)
{
var db = Sugar.GetInstance();
//新增表
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(TestModel));
Console.ReadLine();
}
3.实体不使用特性
创建对象的时候,根据规则,指定哪个字段主键,哪个字段自增;这样就不需要在实体添加特性
var db= new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
ConnectionString = Config.ConnectionString,
sAutoCloseConnection = true,
ConfigureExternalServices=new ConfigureExternalServices() {
EntityService = (t, column) =>
{
if (column.PropertyName.ToLower() == "id") //是id的设为主键
{
column.IsPrimarykey = true;
if (column.PropertyInfo.PropertyType == typeof(int)) //是id并且是int的是自增
{
column.IsIdentity = true;
}
}
}
}
});
4.特性明细
下面是CRUD用到的特性,不包含建表的属性
名称 | 描述 |
---|---|
IsIdentity | 自增列 |
IsPrimaryKey | 创建主键 |
ColumnName | 实体类属性和数据库列名不同时,设置数据库列名 |
IsIgnore | ORM不处理该列 [即忽略] |
IsOnlyIgnoreInsert | 插入操作时不处理该列 [插入时忽略] |
IsOnlyIgnoreUpdate | 更新操作时不处理该列 [更新时忽略] |
OracleSequenceName | 设置Oracle序列,设置后该列等同于自增列 |
ColumnDescription | 备注 |
Length | 长度 |
IsNullable | 是否可以为Null;默认False(允许为NULL) |
DecimalDigits | 精度;decimal(18,2),Length=19,DecimalDigits=2 |
OldColumnName | 修改列名用,这样不会新增或删除列 |
IsDisabledDelete | 禁止删除列 |
IsDisabledUpdateAll | 禁止所有更新表的操作 |
ColumnDataType | 字段类型 |
注意:
- EntityInfo:关于数据库表的一些属性
- EntityColumnInfo:关于数据库表字段的一些属性