-
Notifications
You must be signed in to change notification settings - Fork 862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FluentApi 能否实现自定义特性(表前辍) #1247
Comments
补充一下,是针对postgresql场景,发现不但表名Name失效,连列名也失效了, fsql.Aop.ConfigEntityProperty += (s, e) => { var attr = e.Property.GetCustomAttributes(typeof(MyColumnAttribute), false).FirstOrDefault() as MyColumnAttribute; if (attr != null && e.ModifyResult.Name != attr.Name) e.ModifyResult.Name = attr.Name; //字段名 }; |
freesql.Aop.ConfigEntity+=(s,e)=>{
var name = e.EntityType.GetCustomAttribute<TableAttribute>()?.Name ?? e.EntityType.Name;
if (name.Contains(".") == false)
e.ModifyResult.Name="xxxxx." + name;
} |
freesql.Aop.ConfigEntity+=(s,e)=>{
var name = e.EntityType.GetCustomAttribute<TableAttribute>()?.Name ?? //特性
freesql.CodeFirst.GetConfigEntity(e.EntityType))?.Name ?? //FluentApi
e.EntityType.Name;
if (name.Contains(".") == false)
e.ModifyResult.Name="xxxxx." + name;
}; |
大佬牛逼,测试可以了,但还是有个问题 。 fsql.Aop.ConfigEntityProperty += (s, e) => { var attr = e.Property.GetCustomAttributes(typeof(MyColumnAttribute), false).FirstOrDefault() as MyColumnAttribute; if (attr != null && e.ModifyResult.Name != attr.Name) e.ModifyResult.Name = attr.Name; //字段名 }; 这部分FluentApi代码,不知道要怎么实现,查了一下,ICodeFirst好像没有获取ColumnAttributer的方法 |
ColumnAttribute 直接用反射获取 |
看来还是得发个版本,前面给你的方案有个私有成员 v3.2.670-preview22020918 最终方案: freesql.Aop.ConfigEntity += (s, e) =>
{
var name = e.ModifyResult.Name; //可直接获取 FluentApi/Attirubte 生效的内容
if (name.Contains(".") == false)
e.ModifyResult.Name="xxxxx." + name;
};
freesql.Aop.ConfigEntityProperty += (s, e) =>
{
var name = e.ModifyResult.Name; //可直接获取 FluentApi/Attirubte 生效的内容
}; |
1、FreeSqlBuilder Build 时会重新设置 _commonUtils._mappingPriorityTypes 刚才测试结果: [Table(Name = "AAA_attr")]
public class AAA
{
[Column(Name = "aa_attr")]
public int aa { get; set; }
}
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=:memory:")
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
.UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop)
.Build();
fsql.Aop.ConfigEntity += (_, e) =>
{
Console.WriteLine("Aop.ConfigEntity: " + e.ModifyResult.Name);
};
fsql.Aop.ConfigEntityProperty += (_, e) =>
{
Console.WriteLine("Aop.ConfigEntityProperty: " + e.ModifyResult.Name);
};
fsql.Select<AAA>();
fsql.CodeFirst.ConfigEntity<AAA>(t =>
{
t.Name("AAA_fluentapi");
t.Property(a => a.aa).Name("AA_fluentapi");
});
fsql.Select<AAA>(); 打印:
UseMappingPriority 优先级是 Attribute 最小,其次 FluentApi,最大 Aop。 FluentApi 可以复盖 Attribute 设置,Aop 可以复盖 FluentApi 设置。 上面两个为空,是获取 IndexAttribute 造成的,对 TableAttribute 结果没有影响。 第一次 .Select<T> 获取元数据,缓存起来 fluentapi 会移除元数据缓存,因此第二次 .Select<T> 又获取了一次元数据,再缓存起来。 |
Feature 特性
能否在FluentApi中加入自定义特性支持
简要描述原因
受#407 回复启发,在https://github.com/dotnetcore/FreeSql/wiki/FluentApi
中,可以引用默认的特性,而目前因为Table属性会发生失效的情况,通过折中方式实现了。
但个人更倾向于FluentApi,而不是实体特性,这样不对实体类产生太多的侵入性。
使用场景
针对各种个性化特性的场景
The text was updated successfully, but these errors were encountered: