2

コードシステムによるNHibernateの「セクシーな」マッピングを使用して、次の状況をマッピングする方法を見つけようとしています。運が悪いので、しばらくの間これを理解しようとしていたので、助けてください! コンポーネントを使用して複合キーを表しています。以下は、私がマッピングしようとしているテーブルです。

Account
-------
BSB (PK)
AccountNumber (PK)
Name

AccountCard
-----------
BSB (PK, FK)
AccountNumber (PK, FK)
CardNumber (PK, FK)

Card
------------
CardNumber (PK)
Status

これが私の現在の試みです(まったく機能していません!)

アカウント:

public class Account
{
    public virtual AccountKey Key { get; set; }
    public virtual float Amount { get; set; }
    public ICollection<Card> Cards { get; set; }
}

public class AccountKey
{
    public virtual int BSB { get; set; }
    public virtual int AccountNumber { get; set; }
    //Equality members omitted
}

public class AccountMapping : ClassMapping<Account>
{
    public AccountMapping()
    {
        Table("Accounts");
        ComponentAsId(x => x.Key, map => 
            {
                map.Property(p => p.BSB);
                map.Property(p => p.AccountNumber);
            });
        Property(x => x.Amount);

        Bag(x => x.Cards, collectionMapping =>
                {
                    collectionMapping.Table("AccountCard");
                    collectionMapping.Cascade(Cascade.None);

                    //How do I map the composite key here?
                    collectionMapping.Key(???);                        
                },
                map => map.ManyToMany(p => p.Column("CardId")));

    }
}

カード:

public class Card
{
    public virtual CardKey Key { get; set; }
    public virtual string Status{ get; set; }

    public ICollection<Account> Accounts { get; set; }
}

public class CardKey
{
    public virtual int CardId { get; set; }
    //Equality members omitted
}

public class CardMapping : ClassMapping<Card>
{
    public CardMapping ()
    {
        Table("Cards");
        ComponentAsId(x => x.Key, map => 
            {
                map.Property(p => p.CardId);
            });
        Property(x => x.Status);

        Bag(x => x.Accounts, collectionMapping =>
        {
            collectionMapping.Table("AccountCard");
            collectionMapping.Cascade(Cascade.None);
            collectionMapping.Key(k => k.Column("CardId"));
        },

        //How do I map the composite key here?
        map => map.ManyToMany(p => p.Column(???)));

    }
}

これが可能であることを教えてください!

4

1 に答える 1

2

あなたはかなり近かった。

とメソッドIKeyMapperの両方のActionパラメーターで取得するのは、必要な数のパラメーターを受け取るメソッドを持っているため、次のようになります。KeyManyToManyColumns

collectionMapping.Key(km => km.Columns(cm => cm.Name("BSB"),
                                       cm => cm.Name("AccountNumber")));
//...
map => map.ManyToMany(p => p.Columns(cm => cm.Name("BSB"),
                                     cm => cm.Name("AccountNumber"))));
于 2012-10-12T03:06:31.567 に答える