C# 流畅配置,忽略基于修饰属性的属性
我需要忽略基于属性的属性 下面您将看到一些代码(我的项目的一个小摘录),您将了解我正在尝试完成的工作 我正在努力将此规则添加到bhibernate配置中,即,如果属性具有C# 流畅配置,忽略基于修饰属性的属性,c#,fluent-nhibernate,C#,Fluent Nhibernate,我需要忽略基于属性的属性 下面您将看到一些代码(我的项目的一个小摘录),您将了解我正在尝试完成的工作 我正在努力将此规则添加到bhibernate配置中,即,如果属性具有[IgnoreProperty]属性,则flientnhibernate不应将其包含在生成的架构中 我希望这是尽可能通用的,这样我就不必为每个实体创建类映射 有没有办法将此类功能直接包含到配置中?如果是这样的话,我真的很想举个例子 public class Person { public virtual string N
[IgnoreProperty]
属性,则flientnhibernate不应将其包含在生成的架构中
我希望这是尽可能通用的,这样我就不必为每个实体创建类映射
有没有办法将此类功能直接包含到配置中?如果是这样的话,我真的很想举个例子
public class Person
{
public virtual string Name { get; set; }
public virtual string Surname { get; set; }
[IgnoreProperty]
public string FullName
{
get { return string.Format("{0} {1}", this.Name, this.Surname); }
}
}
public class IgnorePropertyAttribute : Attribute
{
}
public class SessionManager
{
private static ISessionFactory GetFactory()
{
return
Fluently.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile(Path.Combine(@"C:\temp","MyDatabaseName.db")))
.Mappings(
m =>
m.AutoMappings.Add(AutoMap.AssemblyOf<Person>(t => t.Namespace.StartsWith(typeof(Person).Namespace))))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
}
公共类人物
{
公共虚拟字符串名称{get;set;}
公共虚拟字符串姓氏{get;set;}
[不动产]
公共字符串全名
{
获取{return string.Format(“{0}{1}”,this.Name,this.names);}
}
}
公共类IgnorePropertyAttribute:属性
{
}
公共类会话管理器
{
私有静态ISessionFactory GetFactory()
{
返回
fluent.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile(Path.Combine(@“C:\temp”,“MyDatabaseName.db”))
.映射(
m=>
m、 添加(AutoMap.AssemblyOf(t=>t.Namespace.StartsWith(typeof(Person.Namespace)))
.ExposeConfiguration(构建架构)
.BuildSessionFactory();
}
}
有关详细信息,请参见此
您可以使用谓词忽略属性
.OverrideAll(map =>
{
map.IgnoreProperties(x => x.Name.Contains("Something"));
});
只需修改谓词以使用反射来确定是否定义了属性
更新:
这可以在ClassMap
s中使用。为了在全球范围内应用该约定,请应用实现IPropertyConventionAcceptance
接口的约定(请参见或):
公共MyConvention:IPropertyConventionAcceptance
{
公共无效接受(IAcceptanceCriteria标准)
{
Expect(x=>boolValue);//[是否存在IgnoreAttribute];
}
}
页面将告诉您有关约定以及如何全局应用它们的更多信息。以下代码将阻止在数据库中生成列。不要忘记将配置添加到映射中
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
if (member.MemberInfo.GetCustomAttributes(typeof(IgnorePropertyAttribute), true).Length > 0)
{
return false;
}
return base.ShouldMap(member);
}
}
正如我在问题中提到的,我不知道如何将其添加到我的fluent配置中。这是一段代码,没有关于在何处添加它的参考,也没有显示任何关于如何为属性标识配置
OverideAll
。fluent.configure().Mappings(m=>{m.FluentMappings.Conventions.AddFromAssemblyOf();})
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
if (member.MemberInfo.GetCustomAttributes(typeof(IgnorePropertyAttribute), true).Length > 0)
{
return false;
}
return base.ShouldMap(member);
}
}