SqlSugar/Src/Asp.Net/DmTest/Demo/DemoE_CodeFirst.cs

81 lines
3.3 KiB
C#
Raw Normal View History

2020-10-31 20:00:59 +08:00
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest
{
public class DemoE_CodeFirst
{
public static void Init()
{
Console.WriteLine("");
Console.WriteLine("#### CodeFirst Start ####");
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.Dm,
ConnectionString = Config.ConnectionString3,
InitKeyType = InitKeyType.Attribute,
2022-09-29 19:55:27 +08:00
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
2020-10-31 20:00:59 +08:00
});
// db.DbMaintenance.CreateDatabase();
2022-09-29 19:55:27 +08:00
if (db.DbMaintenance.IsAnyTable("CodeFirstTable1", false))
db.DbMaintenance.DropTable("CodeFirstTable1");
if (db.DbMaintenance.IsAnyTable("CodeFirstLong", false))
db.DbMaintenance.DropTable("CodeFirstLong");
2020-10-31 20:00:59 +08:00
db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1
db.Insertable(new CodeFirstTable1() { Name = "a", Text="a" }).ExecuteCommand();
var list = db.Queryable<CodeFirstTable1>().ToList();
2022-09-29 19:55:27 +08:00
db.CodeFirst.InitTables<CodeFirstLong>();
var tableInfo=db.DbMaintenance.GetColumnInfosByTableName("CodeFirstLong", false);
2022-12-11 19:50:32 +08:00
db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
{
IsAutoToUpper = false
};
db.CodeFirst.InitTables<CodeFirstNoUpper>();
db.Insertable(new CodeFirstNoUpper() { Id = Guid.NewGuid() + "", Name = "a" }).ExecuteCommand();
var list2= db.Queryable<CodeFirstNoUpper>().Where(it=>it.Id!=null).ToList();
db.Updateable(list2).ExecuteCommand();
db.Deleteable(list2).ExecuteCommand();
db.Updateable(list2.First()).ExecuteCommand();
db.Deleteable<CodeFirstNoUpper>().Where(it => it.Id != null).ExecuteCommand();
db.Updateable<CodeFirstNoUpper>().SetColumns(it=>it.Name=="a").Where(it => it.Id != null).ExecuteCommand();
db.Updateable<CodeFirstNoUpper>().SetColumns(it =>new CodeFirstNoUpper() {
Name="a"
} ).Where(it => it.Id != null).ExecuteCommand();
2020-10-31 20:00:59 +08:00
Console.WriteLine("#### CodeFirst end ####");
}
}
2022-12-11 19:50:32 +08:00
public class CodeFirstNoUpper
{
[SugarColumn(IsPrimaryKey =true)]
public string Id { get; set; }
public string Name { get; set; }
}
2020-10-31 20:00:59 +08:00
2022-09-29 19:55:27 +08:00
public class CodeFirstLong
{
[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
public long id { get; set; }
}
2020-10-31 20:00:59 +08:00
public class CodeFirstTable1
{
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public int Id { get; set; }
public string Name { get; set; }
[SugarColumn(ColumnDataType = "varchar(255)")]//custom
public string Text { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime CreateTime { get; set; }
}
}