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

125 lines
4.4 KiB
C#
Raw Normal View History

2019-06-04 21:00:10 +08:00
using SqlSugar;
2023-05-05 22:16:31 +08:00
using SqlSugar.DbConvert;
2019-06-04 21:00:10 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2023-06-05 22:26:51 +08:00
using System.Net.Http.Headers;
2019-06-04 21:00:10 +08:00
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.Oracle,
ConnectionString = Config.ConnectionString3,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
2023-07-14 15:05:29 +08:00
db.CodeFirst.InitTables<T_DICT>();
db.Updateable(new T_DICT() { LABEL = "a", TYPE = "a", TYPENAME = "a", VALUE = "a" })
.ExecuteCommand();
2023-05-05 22:16:31 +08:00
db.Aop.OnLogExecuting = (s, p) => Console.WriteLine(UtilMethods.GetNativeSql(s,p));
2019-06-04 21:00:10 +08:00
db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1
db.Insertable(new CodeFirstTable1() { Name = "a", Text="a" }).ExecuteCommand();
var list = db.Queryable<CodeFirstTable1>().ToList();
2023-05-08 22:29:00 +08:00
db.CodeFirst.InitTables<CodeFirstTable22x2>();
db.Updateable(new List<CodeFirstTable22x2>() { new CodeFirstTable22x2() { Name = "a" },new CodeFirstTable22x2() { Name = "a" } })
2023-05-05 22:16:31 +08:00
.ExecuteCommand();
2023-05-08 22:29:00 +08:00
db.Updateable( new CodeFirstTable22x2() { Name = "a" } )
2023-05-08 21:58:38 +08:00
.ExecuteCommand();
2023-05-08 22:29:00 +08:00
db.Insertable(new List<CodeFirstTable22x2>() { new CodeFirstTable22x2() { Name = "a" }, new CodeFirstTable22x2() { Name = "a" } })
2023-05-05 22:16:31 +08:00
.ExecuteCommand();
2023-05-08 22:29:00 +08:00
db.Insertable(new CodeFirstTable22x2() { Name = "a" })
2023-05-08 21:58:38 +08:00
.ExecuteCommand();
2023-05-08 22:29:00 +08:00
var list2=db.Queryable<CodeFirstTable22x2>().ToList();
2023-06-05 22:26:51 +08:00
db.CodeFirst.InitTables<CodeFirstUnitrew>();
db.Insertable(new CodeFirstUnitrew() { Index = 1 }).ExecuteCommand();
2023-08-30 20:04:57 +08:00
db = NewUnitTest.Db;
db.CodeFirst.InitTables<Unit00Z11C12>();
db.DbMaintenance.TruncateTable<Unit00Z11C12>();
db.Insertable(new Unit00Z11C12() { type = UnitType.a, type2 = UnitType.b }).ExecuteCommand();
var list3=db.Queryable<Unit00Z11C12>().Select(it => new DTO
{
unit00Z11C12 = it
}).ToList();
2019-06-04 21:00:10 +08:00
Console.WriteLine("#### CodeFirst end ####");
}
}
2023-08-30 20:04:57 +08:00
public enum UnitType
{
a=0,
b=2
}
public class DTO
{
public Unit00Z11C12 unit00Z11C12 { get; set; }
}
public class Unit00Z11C12
{
[SqlSugar.SugarColumn( IsNullable = true)]
public UnitType type { get; set; }
[SqlSugar.SugarColumn( IsNullable = true)]
public UnitType? type2 { get; set; }
}
2023-07-14 15:05:29 +08:00
/// <summary>
/// 测试表
/// </summary>
[SugarTable("T_DICT")]
public class T_DICT
{
/// <summary>
/// Desc:字典类型
/// Default:
/// Nullable:False
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public string TYPE { get; set; } = "";
/// <summary>
/// Desc:字典名称
/// Default:
/// Nullable:True
/// </summary>
public string? TYPENAME { get; set; }
/// <summary>
/// Desc:字典键值
/// Default:
/// Nullable:False
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public string VALUE { get; set; } = "";
/// <summary>
/// Desc:字典标签
/// Default:
/// Nullable:False
/// </summary>
public string? LABEL { get; set; }
}
2023-06-05 22:26:51 +08:00
public class CodeFirstUnitrew
{
public int Index { get; set; }
}
2023-05-08 22:29:00 +08:00
[SugarTable("CodeFirstTable22r2")]
public class CodeFirstTable22x2
2023-05-05 22:16:31 +08:00
{
[SugarColumn(OracleSequenceName = "SEQ_ID", IsPrimaryKey = true)]
public int Id { get; set; }
2023-05-08 22:29:00 +08:00
[SugarColumn( SqlParameterDbType =typeof(Nvarchar2PropertyConvert) )]
2023-05-05 22:16:31 +08:00
public string Name { get; set; }
}
2019-06-04 21:00:10 +08:00
public class CodeFirstTable1
{
[SugarColumn(OracleSequenceName ="SEQ_ID", IsPrimaryKey = true)]
public int Id { get; set; }
public string Name { get; set; }
public string Text { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime CreateTime { get; set; }
}
}