时间:2021-07-01 10:21:17 帮助过:7人阅读
public SqlSugarClient Db; public AppDbContext() { Db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "server=.;database=StudentDb;uid=sa;pwd=123;", DbType = DbType.SqlServer,//设置数据库类型 IsAutoCloseConnection = true,//自动释放数据库,如果存在事务,在事务结束之后释放。 InitKeyType = InitKeyType.Attribute//从实体特性中读取主键自增列信息 }); Db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + "\r\n" + Db.Utilities.SerializeObject (pars.ToDictionary(it => it.ParameterName, it => it.Value))); Console.WriteLine(); }; } public void CreateTable(bool Backup=false,int StringDefaultLength=50,params Type[] types) { Db.CodeFirst.SetStringDefaultLength(StringDefaultLength); Db.DbMaintenance.CreateDatabase(); if (Backup) { Db.CodeFirst.BackupTable().InitTables(types); } else { Db.CodeFirst.InitTables(types); } } public SimpleClient<Students> studentDb { get { return new SimpleClient<Students>(Db); } } public SimpleClient<Schools> schoolDb { get { return new SimpleClient<Schools>(Db); } }
[SugarTable("Students")] public class Students { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//如果是主键,此处必须指定,否则会引发InSingle(id)方法异常。 public int Id { get; set; } public string StudentName { get; set; } public string Class { get; set; } public int age { get; set; } public DateTime Time { get; set; } }
[SugarTable("Schools")] public class Schools { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//如果是主键,此处必须指定,否则会引发InSingle(id)方法异常。 public int SId { get; set; } public string SchoolName { get; set; } }
public class Tests { [SetUp] public void Setup() { } [Test] public void Test1() { AppDbContext context = new AppDbContext(); context.CreateTable(false, 50, typeof(Students), typeof(Schools)); } }
Sqlsugar中使用Codefrist创建数据库
标签:efault setup 异常 pre ict 方法 ram time return