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

150 lines
5.6 KiB
C#
Raw Normal View History

2019-05-07 13:17:27 +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()
{
2019-05-07 15:25:55 +08:00
Console.WriteLine("");
Console.WriteLine("#### CodeFirst Start ####");
2019-05-07 13:17:27 +08:00
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
2019-06-02 10:11:07 +08:00
ConnectionString = Config.ConnectionString3,
2019-05-07 13:17:27 +08:00
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
2022-05-04 11:34:08 +08:00
db.Aop.OnLogExecuted = (s, p) =>
{
Console.WriteLine(s);
};
db.DbMaintenance.CreateDatabase();
2019-05-07 13:17:27 +08:00
db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1
2022-05-04 11:34:08 +08:00
db.DbMaintenance.TruncateTable<CodeFirstTable1>();
db.Insertable(new CodeFirstTable1() { Name = "a", Text="a",CreateTime=DateTime.Now }).ExecuteCommand();
db.Insertable(new CodeFirstTable1() { Name = "a", Text = "a", CreateTime = DateTime.Now.AddDays(1) }).ExecuteCommand();
2019-05-17 20:49:22 +08:00
var list = db.Queryable<CodeFirstTable1>().ToList();
2022-06-15 13:31:14 +08:00
db.CodeFirst.InitTables<UnituLong>();
db.Insertable(new UnituLong() { longx = 1 }).ExecuteCommand();
var ulList=db.Queryable<UnituLong>().Where(x => x.longx > 0).ToList();
2022-08-20 13:42:46 +08:00
db.CodeFirst.As<UnituLong>("UnituLong0011").InitTables<UnituLong>();
2019-05-07 15:25:55 +08:00
Console.WriteLine("#### CodeFirst end ####");
2022-11-06 12:28:43 +08:00
db.CodeFirst.InitTables<Unituadfasf1>();
2022-11-11 12:46:37 +08:00
db.CodeFirst.InitTables<CodeFirstTable111>();
db.Queryable<CodeFirstTable111>()
.Select(it => new CodeFirstTable111()
{
Name = it.CreateTime.HasValue ? it.CreateTime.Value.ToString("yyyy-MM-dd") : string.Empty
}).ToList();
2022-11-14 01:03:01 +08:00
db.CodeFirst.InitTables<CodeFirstimg>();
2023-01-11 19:08:39 +08:00
if (db.DbMaintenance.IsAnyTable("CodeFirstMaxString1",false))
db.DbMaintenance.DropTable<CodeFirstMaxString1>();
db.CodeFirst.InitTables<CodeFirstMaxString1>();
2023-01-12 17:40:56 +08:00
db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
{
EntityService = (s, p) =>
{
p.IfTable<UnitIFTable>()
.UpdateProperty(it=>it.id,it =>
{
it.IsIdentity = true;
it.IsPrimarykey= true;
})
.UpdateProperty(it => it.Name, it=>{
it.Length = 100;
it.IsNullable = true;
})
.OneToOne(it=>it.UnitIFTableInfo,nameof(UnitIFTable.id));
}
};
db.CodeFirst.InitTables<UnitIFTable>();
2023-02-06 23:29:15 +08:00
db.CodeFirst.InitTables<Unittest1011, Unittest22221>();
db.Insertable(new Unittest1011() { name = "a" }).ExecuteCommand();
db.Insertable(new Unittest22221() { name = "a" }).ExecuteCommand();
2022-11-11 12:46:37 +08:00
Console.WriteLine("#### CodeFirst end ####");
2019-05-07 13:17:27 +08:00
}
}
2023-02-06 23:29:15 +08:00
public class Unittest22221
{
[SugarColumn(DefaultValue = " newsequentialid()")]
public Guid id { get; set; }
[SugarColumn(IsNullable = true)]
public string name { get; set; }
}
public class Unittest1011
{
[SugarColumn(DefaultValue ="newid()")]
public Guid id { get; set; }
[SugarColumn(IsNullable =true)]
public string name { get; set; }
}
2023-01-12 17:40:56 +08:00
public class UnitIFTable
{
//[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
public int id { get; set; }
// [SugarColumn(IsNullable =true)]
public string Name { get; set; }
public UnitIFTable UnitIFTableInfo { get; set; }
}
2023-01-11 19:08:39 +08:00
public class CodeFirstMaxString1
{
2023-01-11 19:25:42 +08:00
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
2023-01-11 19:08:39 +08:00
public string img { get; set; }
}
2022-11-14 01:03:01 +08:00
public class CodeFirstimg
{
[SugarColumn(Length =100)]
public byte[] img { get; set; }
}
2022-11-11 12:46:37 +08:00
public class CodeFirstTable111
{
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public int Id { get; set; }
public string Name { get; set; }
[SugarColumn(ColumnDataType = "Nvarchar(255)")]//custom
public string Text { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime? CreateTime { get; set; }
}
2022-11-06 12:28:43 +08:00
[SugarIndex("IndexUnituadfasf1_longx{include:name,id}", nameof(longx), OrderByType.Asc)]
public class Unituadfasf1
{
public ulong longx { get; set; }
public int id { get; set; }
public string name { get; set; }
}
2019-05-07 13:17:27 +08:00
2022-06-15 13:31:14 +08:00
public class UnituLong
{
public ulong longx{get;set;}
}
2022-05-04 11:34:08 +08:00
[SugarIndex("index_codetable1_name",nameof(CodeFirstTable1.Name),OrderByType.Asc)]
[SugarIndex("index_codetable1_nameid", nameof(CodeFirstTable1.Name), OrderByType.Asc,nameof(CodeFirstTable1.Id),OrderByType.Desc)]
[SugarIndex("unique_codetable1_CreateTime", nameof(CodeFirstTable1.CreateTime), OrderByType.Desc,true)]
2019-05-07 13:17:27 +08:00
public class CodeFirstTable1
{
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public int Id { get; set; }
public string Name { get; set; }
[SugarColumn(ColumnDataType = "Nvarchar(255)")]//custom
public string Text { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime CreateTime { get; set; }
}
}