在code First的情况下
public static void CodeFirstShow()
{
//配置连接数据
ConnectionConfig connectionConfig = new ConnectionConfig();
//通过静态类获取连接字符串,传给connectionConfig
connectionConfig.ConnectionString = SqlSugarConnectionString.GetConnectionString1();
//是否自动关闭连接
connectionConfig.IsAutoCloseConnection = true;
//设置sql服务器类型
connectionConfig.DbType = DbType.SqlServer;
//打开数据库连接
using (SqlSugarClient sqlSugarClient = new SqlSugarClient(connectionConfig))
{
{
///检查表是否存在,不存在就创建
sqlSugarClient.DbMaintenance.CreateDatabase();
}
///创建两个表
///这里的CodeFirstTable1和CodeFirstTable2是提前准备的两个class
//{
// sqlSugarClient.CodeFirst.InitTables(typeof(CodeFirstTable1), typeof(CodeFirstTable2));
//}
///批量创建表 此处是根据反射获取程序集所有类的信息,遍历这些信息,创建数据库表
{
////获取Models的Type类型
//Type[] types = Assembly
// .Load("Zzy.SqlSugar.Models")
// .GetTypes().Where(type =>type.FullName.Contains("Code"))
// .ToArray();
////判断是否存在表
//foreach (Type type in types)
//{
// if (sqlSugarClient.DbMaintenance.IsAnyTable(type.FullName))
// {
// sqlSugarClient.DbMaintenance.DropTable(type.FullName);
// }
//}
////然后创建表
//sqlSugarClient.CodeFirst.InitTables(types);
}
//动态设置表名 可在字符串中修改创建表的名称
//{
// sqlSugarClient.CodeFirst.As<UnituLong0011>("UnituLong0012").InitTables<UnituLong0011>();
//}
}
}