SqlSugar/Src/Asp.NetCore2/PgSqlTest/UserTestCases/UCodeFirst.cs
2024-06-22 18:34:41 +08:00

116 lines
4.0 KiB
C#

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public partial class NewUnitTest
{
public static void CodeFirst()
{
if (Db.DbMaintenance.IsAnyTable("UnitCodeTest1", false))
Db.DbMaintenance.DropTable("UnitCodeTest1");
Db.CodeFirst.InitTables<UnitCodeTest1>();
Db.CodeFirst.InitTables<UnitIndextest>();
var db = Db;
db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
{
PgSqlIsAutoToLower = false,
PgSqlIsAutoToLowerCodeFirst = false
};
db.CodeFirst.InitTables<UpperOrder, UpperItem>();
var list = db.Queryable<UpperOrder>().LeftJoin<UpperOrder>((X1, Y1) =>
X1.Id == Y1.Id)
.Where(X1 => X1.Id == 1)
.Select(X1 => new {
x1 = X1.Id,
x2 = X1.Name
}).ToList();
var list2 = db.Queryable<UpperOrder>().LeftJoin<UpperItem>((X1, Y1) =>
X1.Id == Y1.Id)
.Where(X1 => X1.Id == 1)
.Select<VUpperOrder>().ToList();
db = NewUnitTest.Db;
db.CodeFirst.InitTables<Unitadfafay1>();
db.DbMaintenance.TruncateTable<Unitadfafay1>();
db.Insertable(new Unitadfafay1() { a = "a" }).ExecuteCommand();
db.CodeFirst.InitTables<UNITADFAFAY1>();
var bvalue=db.Queryable<UNITADFAFAY1>().First().b;
if (bvalue != "b")
{
throw new Exception("unit error");
}
}
public class Unitadfafay1
{
public string a { get; set; }
}
public class UNITADFAFAY1
{
public string a { get; set; }
[SugarColumn(DefaultValue ="b")]
public string b { get; set; }
}
public class VUpperOrder
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime UpperItemCreateTime { get; set; }
public int UpperItemCustomId { get; set; }
}
public class UpperOrder
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime CreateTime { get; set; }
[SugarColumn(IsNullable = true)]
public int CustomId { get; set; }
[SugarColumn(IsIgnore = true)]
public List<OrderItem> Items { get; set; }
}
public class UpperItem
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime CreateTime { get; set; }
[SugarColumn(IsNullable = true)]
public int CustomId { get; set; }
[SugarColumn(IsIgnore = true)]
public List<OrderItem> Items { get; set; }
}
[SqlSugar.SugarIndex("UnitIndextestIndex", nameof(UnitIndextest.Table), SqlSugar.OrderByType.Asc)]
public class UnitIndextest
{
public string Table { get; set; }
public string Id { get; set; }
}
public class UnitCodeTest1
{
[SqlSugar.SugarColumn(IndexGroupNameList = new string[] { "group1" })]
public int Id { get; set; }
[SqlSugar.SugarColumn(DefaultValue= "now()", IndexGroupNameList =new string[] {"group1" } )]
public DateTime? CreateDate { get; set; }
}
}
}