From 6b86fc46bd6926e605de0abf8a8fc1388e9786dd Mon Sep 17 00:00:00 2001 From: d4ilys <963922242@qq.com> Date: Fri, 5 May 2023 11:06:41 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0DynamicEntity=20-=20Attri?= =?UTF-8?q?bute=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DynamicEntity/DynamicEntityTest.cs | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs b/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs index 9e71b207..8b80a2fc 100644 --- a/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs @@ -3,10 +3,62 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using FreeSql.DataAnnotations; +using FreeSql.Extensions.DynamicEntity; +using Xunit; namespace FreeSql.Tests.DynamicEntity { - internal class DynamicEntityTest + public class DynamicEntityTest { + private static IFreeSql fsql = new FreeSqlBuilder().UseConnectionString(DataType.PostgreSQL, + "Host=192.168.0.36;Port=5432;Username=postgres;Password=123; Database=test;ArrayNullabilityMode=Always;Pooling=true;Minimum Pool Size=1") + .UseMonitorCommand(d => Console.WriteLine(d.CommandText)).Build(); + + + [Fact] + public void NormalTest() + { + Type type = DynamicCompileHelper.DynamicBuilder() + .Class("NormalUsers") + .Property("Id", typeof(int)) + .Property("Name", typeof(string)) + .Property("Address", typeof(string)) + .Build(); + var dict = new Dictionary + { + ["Name"] = "张三", + ["Id"] = 1, + ["Address"] = "北京市" + }; + var instance = DynamicCompileHelper.CreateObjectByType(type, dict); + //根据Type生成表 + fsql.CodeFirst.SyncStructure(type); + fsql.Insert().AsType(type).AppendData(instance).ExecuteAffrows(); + } + + [Fact] + public void AttributeTest() + { + Type type = DynamicCompileHelper.DynamicBuilder() + .Class("AttributeUsers", new TableAttribute() { Name = "T_Attribute_User" }, + new IndexAttribute("Name_Index", "Name", false)) + .Property("Id", typeof(int), + new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 }) + .Property("Name", typeof(string), + new ColumnAttribute() { StringLength = 20, Position = 2 }) + .Property("Address", typeof(string), + new ColumnAttribute() { StringLength = 150, Position = 3 }) + .Build(); + var dict = new Dictionary + { + ["Name"] = "张三", + ["Address"] = "北京市" + }; + var instance = DynamicCompileHelper.CreateObjectByType(type, dict); + //根据Type生成表 + fsql.CodeFirst.SyncStructure(type); + fsql.Insert().AsType(type).AppendData(instance).ExecuteAffrows(); + } } -} +} \ No newline at end of file