我正在使用符合性映射开始一个新项目。我将一组共同的字段拉出到组件映射中:
public class AuditTrailMap : ComponentMapping<AuditTrail>
{
public AuditTrailMap()
{
Property(x => x.Created, xm => xm.NotNullable(true));
Property(x => x.Updated, xm => xm.NotNullable(true));
Property(x => x.UpdatedBy, xm => { xm.NotNullable(true); xm.Length(128); });
Property(x => x.UpdatedBySID, xm => { xm.NotNullable(true); xm.Length(128); });
Property(x => x.OnBehalfOf, xm => { xm.NotNullable(true); xm.Length(128); });
Property(x => x.OnBehalfOfSID, xm => { xm.NotNullable(true); xm.Length(128); });
}
}
然而,在我的类映射中,似乎没有任何重载的Component方法会接受其中一个,也不会神奇地拾取它。我如何使用此映射?
答案 0 :(得分:0)
我会创建一个有助于映射组件的扩展方法:
public static class AuditTrailMapingExtensions
{
public static void AuditTrailComponent<TEntity>(
this PropertyContainerCustomizer<TEntity> container,
Func<TEntity, AuditTrail> property)
{
container.Component(
property,
comp =>
{
comp.Property(x => x.Created, xm => xm.NotNullable(true));
comp.Property(x => x.Updated, xm => xm.NotNullable(true));
comp.Property(x => x.UpdatedBy, xm => { xm.NotNullable(true); xm.Length(128); });
comp.Property(x => x.UpdatedBySID, xm => { xm.NotNullable(true); xm.Length(128); });
comp.Property(x => x.OnBehalfOf, xm => { xm.NotNullable(true); xm.Length(128); });
comp.Property(x => x.OnBehalfOfSID, xm => { xm.NotNullable(true); xm.Length(128); });
});
}
然后像这样使用它:
class MyEntityDbMapping : ClassMapping<MyEntity>
{
public MyEntityDbMapping ()
{
// ...
this.AuditTrailComponent(x => x.AuditTrail);
}
}
这种帮助方法有一个很大的优点:您可以添加其他参数,例如有条件地设置非空约束。