2

私は適合マッピングを使用して新しいプロジェクトを開始しています。次のように、コンポーネント マッピングに引き出された共通のフィールド セットがあります。

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 メソッドのオーバーロードはないようで、魔法のようにピックアップされません。このマッピングを使用するにはどうすればよいですか?

4

1 に答える 1

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);
    }
}

このような支援方法には大きな利点があります。たとえば、null以外の制約を条件付きで設定するために、引数を追加できます。

于 2012-10-08T10:15:15.683 に答える