using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ZhaoxiFlower.Model.Entitys
{public class Users{/// /// 主键/// [SugarColumn(IsPrimaryKey =true,IsIdentity =true)]public int Id { get; set; }/// /// 用户名/// [SugarColumn(IsNullable = false)]public string UserName { get; set; }/// /// 昵称/// [SugarColumn(IsNullable = false)]public string NickName { get; set; }/// /// 用户类型/// [SugarColumn(IsNullable = false)]public int UserType { get; set; }/// /// 密码/// [SugarColumn(IsNullable = false)]public string Password { get; set; }/// /// 验证码Key/// [SugarColumn(IsNullable = false)]public string ValidateKey { get; set; }/// /// 验证码Code/// [SugarColumn(IsNullable = false)]public string ValidateCode { get; set; }/// /// 创建时间/// [SugarColumn(IsNullable = false)]public DateTime CreateTime { get; set; }}
}
using SqlSugar;
using System.Reflection;
using ZhaoxiFlower.Model.Entitys;
using static System.Net.WebRequestMethods;namespace ZhaoxiFlower.model
{public class DbContext{public static SqlSugarClient db = new SqlSugarClient(new ConnectionConfig(){ConnectionString = "database='" + "ZhaoxiFlowerDb" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "mysql123456JRH@" + "';charset='utf8';pooling=true",//ConnectionString = "Data Source=mysql;Initial Catalog=ZhaoxiFlowerDb;Persist Secrity Info=True;User ID=sa;Password=123456",//链接字符串DbType=DbType.MySql,//数据库类型IsAutoCloseConnection=true//不设置true,需要手动关闭close});public static void InitDataBase(){//初始化数据:如果不存在则创建数据库db.DbMaintenance.CreateDatabase();//创建表string nspace = "ZhaoxiFlower.Model.Entitys";Type[] ass = Assembly.LoadFrom("bin/Debug/net6.0/ZhaoxiFlower.Model.dll").GetTypes().Where(p => p.Namespace == nspace).ToArray();db.CodeFirst.SetStringDefaultLength(200).InitTables(ass);写入测试数据//List flowers = new List()//{//#region 爱情鲜花// new Flower()// {// Title="韩系系列/公主的假日",// Image="https://img2.baidu.com/it/u=1527695877,732598238&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=667",// BigImage="",// Description="[甜美的公主,今晚是你最美]粉色玫瑰11支,白色洋桔梗5支,大叶尤佳黎10支",// Price=200,// Language="娇羞的粉色",// Material="韩式花束系列",// Packing="烟粉色",// DeliveryRemarks="江浙沪包邮",// Type=1// },// new Flower()// {// Title="韩系系列/公主的假日",// Image="https://img1.baidu.com/it/u=2560182386,4141543814&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=747",// BigImage="",// Description="[甜美的公主,今晚是你最美]粉色玫瑰11支,白色洋桔梗5支,大叶尤佳黎10支",// Price=200,// Language="娇羞的粉色",// Material="韩式花束系列",// Packing="烟粉色",// DeliveryRemarks="江浙沪包邮",// Type=1// },// new Flower()// {// Title="韩系系列/公主的假日",// Image="https://img0.baidu.com/it/u=2888899553,4073729271&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500",// BigImage="",// Description="[甜美的公主,今晚是你最美]粉色玫瑰11支,白色洋桔梗5支,大叶尤佳黎10支",// Price=200,// Language="娇羞的粉色",// Material="韩式花束系列",// Packing="烟粉色",// DeliveryRemarks="江浙沪包邮",// Type=1// },// new Flower()// {// Title="韩系系列/公主的假日",// Image="https://img2.baidu.com/it/u=673726950,2545855308&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",// BigImage="",// Description="[甜美的公主,今晚是你最美]粉色玫瑰11支,白色洋桔梗5支,大叶尤佳黎10支",// Price=200,// Language="娇羞的粉色",// Material="韩式花束系列",// Packing="烟粉色",// DeliveryRemarks="江浙沪包邮",// Type=1// },// new Flower()// {// Title="韩系系列/公主的假日",// Image="https://img1.baidu.com/it/u=1394927221,885232140&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=667",// BigImage="",// Description="[甜美的公主,今晚是你最美]粉色玫瑰11支,白色洋桔梗5支,大叶尤佳黎10支",// Price=200,// Language="娇羞的粉色",// Material="韩式花束系列",// Packing="烟粉色",// DeliveryRemarks="江浙沪包邮",// Type=1// },// new Flower()// {// Title="韩系系列/公主的假日",// Image="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.alicdn.com%2Ftfscom%2Fi4%2F2879088530%2FTB2Vq60cuSSBuNjy0FlXXbBpVXa_%21%212879088530.jpg&refer=http%3A%2F%2Fimg.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1673563588&t=00f8d3d10b435a4a7e19921735f9c3aa",// BigImage="",// Description="[甜美的公主,今晚是你最美]粉色玫瑰11支,白色洋桔梗5支,大叶尤佳黎10支",// Price=200,// Language="娇羞的粉色",// Material="韩式花束系列",// Packing="烟粉色",// DeliveryRemarks="江浙沪包邮",// Type=1// },// new Flower()// {// Title="韩系系列/公主的假日",// Image="https://img1.baidu.com/it/u=1394927221,885232140&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=667",// BigImage="",// Description="[甜美的公主,今晚是你最美]粉色玫瑰11支,白色洋桔梗5支,大叶尤佳黎10支",// Price=200,// Language="娇羞的粉色",// Material="韩式花束系列",// Packing="烟粉色",// DeliveryRemarks="江浙沪包邮",// Type=1// },// new Flower()// {// Title="韩系系列/公主的假日",// Image="https://img2.baidu.com/it/u=3030320993,2145269410&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500",// BigImage="",// Description="[甜美的公主,今晚是你最美]粉色玫瑰11支,白色洋桔梗5支,大叶尤佳黎10支",// Price=200,// Language="娇羞的粉色",// Material="韩式花束系列",// Packing="烟粉色",// DeliveryRemarks="江浙沪包邮",// Type=1// }// #endregion//};//db.Insertable(flowers).ExecuteCommand();}}
}