ama*_*ers 7 nhibernate configuration abstract-class fluent automapping
鉴于以下类别:
public class Address : Place
{
public virtual string Street { get; set; }
public virtual int Number { get; set; }
public override string WhereAmI
{
get { string.Format("{0} {1}", Street , Number); }
}
}
public abstract class Place : DomainEntity
{
public abstract string WhereAmI { get; }
}
Run Code Online (Sandbox Code Playgroud)
当我使用此映射时:
var autoMap = AutoMap.AssemblyOf<Party>()
.Override<Place>(map => map.IgnoreProperty(p => p.WhereAmI))
.Override<Address>(map => map.IgnoreProperty(p => p.WhereAmI))
.Where(type => type.Namespace != null && type.Namespace.Contains("Models"));
Run Code Online (Sandbox Code Playgroud)
我仍然得到错误:无法在"地址"类中找到属性'WhereAmI'的setter
我做的事情:
有没有办法让它工作,然后使用接口?
我尝试在 FluentNHibernate 代码中准确追踪当被忽略的属性来自基类时 IgnoreProperty 似乎会崩溃的原因,但时间不够了。如果仅获取属性不是来自基类,那么它似乎工作得很好。
无论如何,您的情况的解决方案似乎是通过继承 DefaultAutomappingConfiguration 创建自定义 IAutomappingConfiguration。请参阅此堆栈溢出答案:How can I create a Fluent NHibernate Convention thatignsingproperties that don't have setters。
这是我成功用于自动映射您提供的示例实体的自定义自动映射配置:
protected class CustomConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap (Member member)
{
if (member.IsProperty && member.IsPublic && !member.CanWrite)
{
return false;
}
return base.ShouldMap(member);
}
public override bool ShouldMap(Type type)
{
return type.Namespace != null && type.Namespace.Contains("Models");
}
}
Run Code Online (Sandbox Code Playgroud)
然后是它的使用:
var autoMap = AutoMap
.AssemblyOf<DomainEntity>(new CustomConfiguration());
Run Code Online (Sandbox Code Playgroud)
请注意,示例中的Where子句必须移至自定义配置类中,因为如果您使用自定义配置实例,则不允许链接它。
| 归档时间: |
|
| 查看次数: |
1463 次 |
| 最近记录: |