目录
使用的相关类
Student
[SugarTable("Student")]//当和数据库名称不一样可以设置表别名 指定表明
public class Student
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增
public int Id { get; set; }
[SugarColumn(ColumnName = "SchoolId", IsNullable = true)]
public int? SchoolId { get; set; }
[SugarColumn(ColumnName = "StudentName", IsNullable = true)]//数据库与实体不一样设置列名
public string? Name { get; set; }
//[SugarColumn(IsOnlyIgnoreInsert = true)]//设置后插入会取数据库默认值
[SugarColumn(ColumnName = "CreateTime", IsNullable = true)]
public DateTime? CreateTime { get; set; }
}
前置配置
using (SqlSugarClient db = new SqlSugarClient(connectionConfig))
{
if (db.DbMaintenance.IsAnyTable("Student"))
{
db.DbMaintenance.DropTable("Student");
}
db.CodeFirst.InitTables(typeof(Student));
List<Student> students = new List<Student>();
for (int i = 0; i < 10; i++)
{
students.Add(new Student()
{
CreateTime = DateTime.Now,
Name = $"测试数据_{i}",
SchoolId = i,
});
}
db.Insertable(students).ExecuteCommand();
///更新表(二)代码在这里实现
}