mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
commit
2164493346
@ -194,8 +194,51 @@ namespace FreeSql.Tests.DynamicEntity
|
|||||||
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
||||||
var objects = fsql.Select<object>().AsType(table.Type).ToList();
|
var objects = fsql.Select<object>().AsType(table.Type).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Issue1591Test()
|
||||||
|
{
|
||||||
|
var backupTableName = "test";
|
||||||
|
var newTableName = "new_test";
|
||||||
|
var key = "index_key";
|
||||||
|
var columns = new List<string>()
|
||||||
|
{
|
||||||
|
"Name",
|
||||||
|
"Tid"
|
||||||
|
};
|
||||||
|
var attributes = new List<Attribute>();
|
||||||
|
attributes.Add(new TableAttribute() { Name = newTableName });
|
||||||
|
|
||||||
|
var indexName = key.ToUpper().Replace(backupTableName.ToUpper(), newTableName.ToUpper());
|
||||||
|
var indexFields = string.Join(",", columns.Select(c => c));
|
||||||
|
var indexAttribute = new IndexAttribute(indexName, indexFields
|
||||||
|
, false);
|
||||||
|
attributes.Add(indexAttribute);
|
||||||
|
|
||||||
|
var table = fsql.CodeFirst.DynamicEntity("AttributeUsers", attributes.ToArray())
|
||||||
|
.Property("Id", typeof(int),
|
||||||
|
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
|
||||||
|
.Property("Name", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 20, Position = 2 })
|
||||||
|
.Property("Tid", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 20, Position = 4 })
|
||||||
|
.Property("Address", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 150, Position = 3 })
|
||||||
|
.Build();
|
||||||
|
var dict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["Name"] = "张三",
|
||||||
|
["Address"] = "北京市"
|
||||||
|
};
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class BaseModel
|
public class BaseModel
|
||||||
{
|
{
|
||||||
[Column(Position = 99)] public DateTime UpdateTime { get; set; }
|
[Column(Position = 99)] public DateTime UpdateTime { get; set; }
|
||||||
|
@ -49,6 +49,7 @@ public static class FreeSqlGlobalDynamicEntityExtensions
|
|||||||
{
|
{
|
||||||
defaultValueInit.Invoke(instance, new object[0]);
|
defaultValueInit.Invoke(instance, new object[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var key in table.ColumnsByCs.Keys)
|
foreach (var key in table.ColumnsByCs.Keys)
|
||||||
{
|
{
|
||||||
if (dict.ContainsKey(key) == false) continue;
|
if (dict.ContainsKey(key) == false) continue;
|
||||||
@ -187,22 +188,32 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
if (tableAttribute == null) continue;
|
if (tableAttribute == null) continue;
|
||||||
|
|
||||||
var classCtorInfo = tableAttribute.GetType().GetConstructor(Type.EmptyTypes);
|
var classCtorInfo = tableAttribute.GetType().GetConstructor(Type.EmptyTypes);
|
||||||
|
var classCtorInfos = tableAttribute.GetType().GetConstructors();
|
||||||
var propertyInfos = tableAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
|
var propertyInfos = tableAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
|
||||||
|
|
||||||
foreach (var propertyInfo in propertyInfos)
|
foreach (var propertyInfo in propertyInfos)
|
||||||
propertyValues.Add(propertyInfo.GetValue(tableAttribute));
|
propertyValues.Add(propertyInfo.GetValue(tableAttribute));
|
||||||
|
//是否存在有参构造函数
|
||||||
//可能存在有参构造
|
var existConstructorArguments = classCtorInfos.Any(c => c.GetParameters().Length > 0);
|
||||||
if (classCtorInfo == null)
|
if (existConstructorArguments)
|
||||||
{
|
{
|
||||||
var constructorTypes = propertyInfos.Select(p => p.PropertyType);
|
var defaultParamsCtor = classCtorInfos.Where(c => c.GetParameters().Length > 0)
|
||||||
classCtorInfo = tableAttribute.GetType().GetConstructor(constructorTypes.ToArray());
|
.OrderBy(c => c.GetParameters().Length).First();
|
||||||
var customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, propertyValues.ToArray());
|
//获取参数默认值
|
||||||
|
var defaultParams = new List<object>();
|
||||||
|
foreach (var parameterInfo in defaultParamsCtor.GetParameters())
|
||||||
|
{
|
||||||
|
defaultParams.Add(parameterInfo.ParameterType.CreateInstanceGetDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
//思路:先通过构造函数的默认值实例化对象,然后通过属性的方式赋值
|
||||||
|
var customAttributeBuilder = new CustomAttributeBuilder(defaultParamsCtor, defaultParams.ToArray(),
|
||||||
|
propertyInfos,
|
||||||
|
propertyValues.ToArray());
|
||||||
typeBuilder.SetCustomAttribute(customAttributeBuilder);
|
typeBuilder.SetCustomAttribute(customAttributeBuilder);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
//不存在构造函数赋值直接属性赋值
|
||||||
var customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[0], propertyInfos,
|
var customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[0], propertyInfos,
|
||||||
propertyValues.ToArray());
|
propertyValues.ToArray());
|
||||||
typeBuilder.SetCustomAttribute(customAttributeBuilder);
|
typeBuilder.SetCustomAttribute(customAttributeBuilder);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user