Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

.net - Fluent code for mapping an IDictionary<SomeEntity, int>?

I'm trying to figure out how to map an IDictionary property in fluent 1.0 RTM.

From my understanding this translates to a ternary association.

Example:

class Bar
{
    public IDictionary<SomeEntity, int> Foo {get; set;}
}

Bar.hbm.xml would then contain:

<map name="Foo" table="BarFooTable">
    <key column="..."/>
    <index-many-to-many class="SomeEntity" column="SomeEntity_Id"/>
    <element column="Value" type="int"/>
</map>

What would I have to write in fluent nhibernate to produce this mapping xml?

The point of interest here is that the key is an entity type while the value is a value type. (edit: At least, this seems to distinguish itself from the various other examples and questions floating around on stackoverflow or google groups, which are value-value or key-key)

After much experimentation I can produce a mapping for an IDictionary<SomeEntity,SomeEntity> (pure entity types):

HasManyToMany(x => x.Foo)
        .AsMap("Key")
        .AsTernaryAssociation("Key2", "Value")
        ; 

I can also produce a mapping for an IDictionary<int,int> (pure value types):

HasMany(x => x.Foo)
    .AsMap<int>("Key")
    .Element("Value")
    ;

I can even get some way to producing a mapping for an IDictionary<int, SomeValue), although none that NHibernate will accept.

But I cannot figure out how to produce a mapping for an IDictionary<SomeValue, int> which is what I want. Can someone provide some tips?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
HasMany(x => x.Foo)
    .KeyColumn("BarId")
    .Element("IntValue")
    .AsMap<SomeEntity>("SomeEntityId")
    .AsTernaryAssociation("SomeEntityId");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...