Skip to content

Commit

Permalink
Merge pull request #1774 from d4ilys/master
Browse files Browse the repository at this point in the history
修复DynamicEntity设置基类后失效问题
  • Loading branch information
2881099 authored Apr 9, 2024
2 parents 1e089af + c1a4ac7 commit 23b81d7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
81 changes: 45 additions & 36 deletions FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -32,15 +41,15 @@ public void NormalTest()
};
var instance = table.CreateInstance(dict);
//根据Type生成表
fsql.CodeFirst.SyncStructure(table.Type);
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = fsql.Select<object>().AsType(table.Type).ToList();
_fsql.CodeFirst.SyncStructure(table.Type);
_fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = _fsql.Select<object>().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),
Expand All @@ -57,15 +66,15 @@ public void AttributeTest()
};
var instance = table.CreateInstance(dict);
//根据Type生成表
fsql.CodeFirst.SyncStructure(table.Type);
var insertId = fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteIdentity();
var select = fsql.Select<object>().AsType(table.Type).ToList();
_fsql.CodeFirst.SyncStructure(table.Type);
var insertId = _fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteIdentity();
var select = _fsql.Select<object>().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),
Expand All @@ -81,23 +90,23 @@ public void SuperClassTest()
};
var instance = table.CreateInstance(dict);
//根据Type生成表
fsql.CodeFirst.SyncStructure(table.Type);
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = fsql.Select<object>().AsType(table.Type).ToList();
_fsql.CodeFirst.SyncStructure(table.Type);
_fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = _fsql.Select<object>().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))
.Property("Id", typeof(int),
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<string, object>
{
Expand All @@ -108,15 +117,15 @@ public void SuperClassVirtualOverrideTest()
};
var instance = table.CreateInstance(dict);
//根据Type生成表
fsql.CodeFirst.SyncStructure(table.Type);
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = fsql.Select<object>().AsType(table.Type).ToList();
_fsql.CodeFirst.SyncStructure(table.Type);
_fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = _fsql.Select<object>().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))
Expand All @@ -135,15 +144,15 @@ public void SuperClassBaseModelAbstractTest()
};
var instance = table.CreateInstance(dict);
//根据Type生成表
fsql.CodeFirst.SyncStructure(table.Type);
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = fsql.Select<object>().AsType(table.Type).ToList();
_fsql.CodeFirst.SyncStructure(table.Type);
_fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = _fsql.Select<object>().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))
Expand All @@ -164,15 +173,15 @@ public void SuperClassBaseModelAbstractAndVirtualTest()
};
var instance = table.CreateInstance(dict);
//根据Type生成表
fsql.CodeFirst.SyncStructure(table.Type);
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = fsql.Select<object>().AsType(table.Type).ToList();
_fsql.CodeFirst.SyncStructure(table.Type);
_fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = _fsql.Select<object>().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)
Expand All @@ -190,9 +199,9 @@ public void DefaultValueTest()
};
var instance = table.CreateInstance(dict);
//根据Type生成表
fsql.CodeFirst.SyncStructure(table.Type);
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = fsql.Select<object>().AsType(table.Type).ToList();
_fsql.CodeFirst.SyncStructure(table.Type);
_fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
var objects = _fsql.Select<object>().AsType(table.Type).ToList();
}

[Fact]
Expand All @@ -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),
Expand All @@ -232,9 +241,9 @@ public void Issue1591Test()
};
var instance = table.CreateInstance(dict);
//根据Type生成表
fsql.CodeFirst.SyncStructure(table.Type);
var insertId = fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteIdentity();
var select = fsql.Select<object>().AsType(table.Type).ToList();
_fsql.CodeFirst.SyncStructure(table.Type);
var insertId = _fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteIdentity();
var select = _fsql.Select<object>().AsType(table.Type).ToList();
}
}

Expand Down
1 change: 1 addition & 0 deletions FreeSql/Extensions/DynamicEntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public DynamicCompileBuilder Property(string propertyName, Type propertyType, bo
public DynamicCompileBuilder Extend(Type superClass)
{
_superClass = superClass;
_typeBuilder.SetParent(superClass);
return this;
}

Expand Down

0 comments on commit 23b81d7

Please sign in to comment.