From a55531b112257b5b3e2c2aeaf4d28031fdd81d3d Mon Sep 17 00:00:00 2001 From: Daily <963922242@qq.com> Date: Tue, 9 Apr 2024 17:21:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3DynamicCompileBuilder=20E?= =?UTF-8?q?xtend=20=E6=96=B9=E6=B3=95=E8=AE=BE=E7=BD=AE=E5=9F=BA=E7=B1=BB?= =?UTF-8?q?=E5=90=8E=20=E5=A4=B1=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DynamicEntity/DynamicEntityTest.cs | 79 +++++++++++-------- FreeSql/Extensions/DynamicEntityExtensions.cs | 1 + 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs b/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs index 113e50799..7aaf04046 100644 --- a/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs @@ -7,19 +7,28 @@ using FreeSql.Extensions.DynamicEntity; using Newtonsoft.Json; using Xunit; +using Xunit.Abstractions; namespace FreeSql.Tests.DynamicEntity { public class DynamicEntityTest { - private static IFreeSql fsql = new FreeSqlBuilder().UseConnectionString(DataType.Sqlite, - "data source=:memory:") - .UseMonitorCommand(d => Console.WriteLine(d.CommandText)).Build(); + private readonly ITestOutputHelper _output; + + private static IFreeSql _fsql; + + public DynamicEntityTest(ITestOutputHelper output) + { + _output = output; + _fsql = new FreeSqlBuilder().UseConnectionString(DataType.Sqlite, + "data source=:memory:") + .UseMonitorCommand(d => _output.WriteLine(d.CommandText)).Build(); + } [Fact] public void NormalTest() { - var table = fsql.CodeFirst.DynamicEntity("NormalUsers") + var table = _fsql.CodeFirst.DynamicEntity("NormalUsers") .Property("Id", typeof(string)) .Property("Name", typeof(string)) .Property("Address", typeof(string)) @@ -32,15 +41,15 @@ public void NormalTest() }; var instance = table.CreateInstance(dict); //根据Type生成表 - fsql.CodeFirst.SyncStructure(table.Type); - fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); - var objects = fsql.Select().AsType(table.Type).ToList(); + _fsql.CodeFirst.SyncStructure(table.Type); + _fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); + var objects = _fsql.Select().AsType(table.Type).ToList(); } [Fact] public void AttributeTest() { - var table = fsql.CodeFirst.DynamicEntity("AttributeUsers", + var table = _fsql.CodeFirst.DynamicEntity("AttributeUsers", new TableAttribute() { Name = "T_Attribute_User" }, new IndexAttribute("Name_Index1", "Name", false)) .Property("Id", typeof(int), @@ -57,15 +66,15 @@ public void AttributeTest() }; var instance = table.CreateInstance(dict); //根据Type生成表 - fsql.CodeFirst.SyncStructure(table.Type); - var insertId = fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteIdentity(); - var select = fsql.Select().AsType(table.Type).ToList(); + _fsql.CodeFirst.SyncStructure(table.Type); + var insertId = _fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteIdentity(); + var select = _fsql.Select().AsType(table.Type).ToList(); } [Fact] public void SuperClassTest() { - var table = fsql.CodeFirst.DynamicEntity("Roles", new TableAttribute() { Name = "T_Role" }, + var table = _fsql.CodeFirst.DynamicEntity("Roles", new TableAttribute() { Name = "T_Role" }, new IndexAttribute("Name_Index2", "Name", false)) .Extend(typeof(BaseModel)) .Property("Id", typeof(int), @@ -81,15 +90,15 @@ public void SuperClassTest() }; var instance = table.CreateInstance(dict); //根据Type生成表 - fsql.CodeFirst.SyncStructure(table.Type); - fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); - var objects = fsql.Select().AsType(table.Type).ToList(); + _fsql.CodeFirst.SyncStructure(table.Type); + _fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); + var objects = _fsql.Select().AsType(table.Type).ToList(); } [Fact] public void SuperClassVirtualOverrideTest() { - var table = fsql.CodeFirst.DynamicEntity("Role_VirtualOverride", + var table = _fsql.CodeFirst.DynamicEntity("Role_VirtualOverride", new TableAttribute() { Name = "T_Role_VirtualOverride" }, new IndexAttribute("Name_Index2", "Name", false)) .Extend(typeof(BaseModelOverride)) @@ -108,15 +117,15 @@ public void SuperClassVirtualOverrideTest() }; var instance = table.CreateInstance(dict); //根据Type生成表 - fsql.CodeFirst.SyncStructure(table.Type); - fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); - var objects = fsql.Select().AsType(table.Type).ToList(); + _fsql.CodeFirst.SyncStructure(table.Type); + _fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); + var objects = _fsql.Select().AsType(table.Type).ToList(); } [Fact] public void SuperClassBaseModelAbstractTest() { - var table = fsql.CodeFirst.DynamicEntity("Role_AbstractOverride", + var table = _fsql.CodeFirst.DynamicEntity("Role_AbstractOverride", new TableAttribute() { Name = "T_Role_AbstractOverride" }, new IndexAttribute("Name_Index2", "Name", false)) .Extend(typeof(BaseModelAbstract)) @@ -135,15 +144,15 @@ public void SuperClassBaseModelAbstractTest() }; var instance = table.CreateInstance(dict); //根据Type生成表 - fsql.CodeFirst.SyncStructure(table.Type); - fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); - var objects = fsql.Select().AsType(table.Type).ToList(); + _fsql.CodeFirst.SyncStructure(table.Type); + _fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); + var objects = _fsql.Select().AsType(table.Type).ToList(); } [Fact] public void SuperClassBaseModelAbstractAndVirtualTest() { - var table = fsql.CodeFirst.DynamicEntity("Role_AbstractAndVirtualOverride", + var table = _fsql.CodeFirst.DynamicEntity("Role_AbstractAndVirtualOverride", new TableAttribute() { Name = "Role_AbstractAndVirtualOverride" }, new IndexAttribute("Name_Index2", "Name", false)) .Extend(typeof(BaseModelAbstractAndVirtual)) @@ -164,15 +173,15 @@ public void SuperClassBaseModelAbstractAndVirtualTest() }; var instance = table.CreateInstance(dict); //根据Type生成表 - fsql.CodeFirst.SyncStructure(table.Type); - fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); - var objects = fsql.Select().AsType(table.Type).ToList(); + _fsql.CodeFirst.SyncStructure(table.Type); + _fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); + var objects = _fsql.Select().AsType(table.Type).ToList(); } [Fact] public void DefaultValueTest() { - var table = fsql.CodeFirst.DynamicEntity("NormalUsers") + var table = _fsql.CodeFirst.DynamicEntity("NormalUsers") .Property("Id", typeof(string)) .Property("Age", typeof(int), false, 12) .Property("Longs", typeof(long), false, 16666) @@ -190,9 +199,9 @@ public void DefaultValueTest() }; var instance = table.CreateInstance(dict); //根据Type生成表 - fsql.CodeFirst.SyncStructure(table.Type); - fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); - var objects = fsql.Select().AsType(table.Type).ToList(); + _fsql.CodeFirst.SyncStructure(table.Type); + _fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteAffrows(); + var objects = _fsql.Select().AsType(table.Type).ToList(); } [Fact] @@ -215,7 +224,7 @@ public void Issue1591Test() , false); attributes.Add(indexAttribute); - var table = fsql.CodeFirst.DynamicEntity("AttributeUsers", attributes.ToArray()) + var table = _fsql.CodeFirst.DynamicEntity("AttributeUsers", attributes.ToArray()) .Property("Id", typeof(int), new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 }) .Property("Name", typeof(string), @@ -232,9 +241,9 @@ public void Issue1591Test() }; var instance = table.CreateInstance(dict); //根据Type生成表 - fsql.CodeFirst.SyncStructure(table.Type); - var insertId = fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteIdentity(); - var select = fsql.Select().AsType(table.Type).ToList(); + _fsql.CodeFirst.SyncStructure(table.Type); + var insertId = _fsql.Insert().AsType(table.Type).AppendData(instance).ExecuteIdentity(); + var select = _fsql.Select().AsType(table.Type).ToList(); } } diff --git a/FreeSql/Extensions/DynamicEntityExtensions.cs b/FreeSql/Extensions/DynamicEntityExtensions.cs index 8a73d08b5..cc8daca85 100644 --- a/FreeSql/Extensions/DynamicEntityExtensions.cs +++ b/FreeSql/Extensions/DynamicEntityExtensions.cs @@ -192,6 +192,7 @@ public DynamicCompileBuilder Property(string propertyName, Type propertyType, bo public DynamicCompileBuilder Extend(Type superClass) { _superClass = superClass; + _typeBuilder.SetParent(superClass); return this; } From c1a4ac705d33f70d3f29bb13b622fad7ca76b806 Mon Sep 17 00:00:00 2001 From: Daily <963922242@qq.com> Date: Tue, 9 Apr 2024 17:24:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B5=8B=E8=AF=95DynamicCompileBuilder=20-?= =?UTF-8?q?=20SuperClassVirtualOverride?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs b/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs index 7aaf04046..3d6254e8c 100644 --- a/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs @@ -106,7 +106,7 @@ public void SuperClassVirtualOverrideTest() new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 }) .Property("Name", typeof(string), new ColumnAttribute() { StringLength = 20, Position = 2 }) - .Property("Operators", typeof(string), true) //重写 virtual 属性 + .Property("Operators", typeof(string), true,new ColumnAttribute() { StringLength = 20} ) //重写 virtual 属性 .Build(); var dict = new Dictionary {