SqlSugar/Src/Asp.NetCore2/ClickHouseTest/Demo/DemoE_CodeFirst.cs

86 lines
3.1 KiB
C#
Raw Normal View History

2022-08-12 20:56:36 +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.ClickHouse,
ConnectionString = Config.ConnectionString3,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
2022-08-14 01:01:16 +08:00
//db.DbMaintenance.CreateDatabase();
2022-08-12 20:56:36 +08:00
db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1
2022-08-14 03:18:19 +08:00
db.Insertable(new CodeFirstTable1() { Name = "a", Text = "a" }).ExecuteCommand();
2022-08-12 20:56:36 +08:00
var list = db.Queryable<CodeFirstTable1>().ToList();
2022-08-14 03:18:19 +08:00
TestBool(db);
TestGuid(db);
2022-08-12 20:56:36 +08:00
Console.WriteLine("#### CodeFirst end ####");
}
2022-08-14 03:18:19 +08:00
private static void TestGuid(SqlSugarClient db)
{
db.CodeFirst.InitTables<GuidTest>();
db.DbMaintenance.TruncateTable("BoolTest");
var Id = 1;
db.Insertable<GuidTest>(new GuidTest() { A = Guid.Empty, Id = Id }).ExecuteCommand();
Console.Write(db.Queryable<GuidTest>().First().A);
db.Updateable<GuidTest>(new GuidTest() { A = Guid.NewGuid(), Id = Id }).ExecuteCommand();
Console.Write(db.Queryable<GuidTest>().First().A);
2022-09-21 14:02:46 +08:00
db.CodeFirst.InitTables<GuidTest22>();
db.Insertable(new GuidTest22() { }).ExecuteReturnSnowflakeId();
var list=db.Queryable<GuidTest22>().ToList();
2022-08-14 03:18:19 +08:00
}
private static void TestBool(SqlSugarClient db)
{
db.CodeFirst.InitTables<BoolTest>();
db.DbMaintenance.TruncateTable("BoolTest");
var Id = 1;
db.Insertable<BoolTest>(new BoolTest() { A = true, Id = Id }).ExecuteCommand();
Console.Write(db.Queryable<BoolTest>().First().A);
db.Updateable<BoolTest>(new BoolTest() { A = false, Id = Id }).ExecuteCommand();
Console.Write(db.Queryable<BoolTest>().First().A);
}
}
2022-09-21 14:02:46 +08:00
public class GuidTest22
2022-09-21 13:47:09 +08:00
{
[SugarColumn(IsPrimaryKey = true)]
public long Id { get; set; }
2022-09-21 14:02:46 +08:00
[SugarColumn(IsNullable = true)]
2022-09-21 13:47:09 +08:00
public Guid? A { get; set; }
2022-09-21 14:02:46 +08:00
[SugarColumn(IsNullable =true)]
public DateTime? Date { get; set; }
2022-09-21 13:47:09 +08:00
}
2022-08-14 03:18:19 +08:00
public class GuidTest
{
[SugarColumn(IsPrimaryKey = true)]
public long Id { get; set; }
public Guid A { get; set; }
}
public class BoolTest
{
[SugarColumn(IsPrimaryKey =true)]
public long Id { get; set; }
public bool A { get; set; }
2022-08-12 20:56:36 +08:00
}
public class CodeFirstTable1
{
2022-08-14 01:01:16 +08:00
[SugarColumn(IsPrimaryKey = true)]
2022-08-12 20:56:36 +08:00
public int Id { get; set; }
public string Name { get; set; }
2022-08-14 01:01:16 +08:00
[SugarColumn(ColumnDataType = "String")]//custom
2022-08-12 20:56:36 +08:00
public string Text { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime CreateTime { get; set; }
}
}