How to map HasMany Dictionary<string, string> ?

Richard Dingwall's Avatar

Richard Dingwall

22 Apr, 2010 07:12 PM via web

public class Team
{

public IDictionary<string, string> Members { get; set; }

}

Trying to map this as:

HasMany(team => team.Members)

.AsMap<string>("Name")
.Element("ProjectRole")
.Cascade.All();

produces the following error:

NHibernate.MappingException : Error mapping generic collection Xxx.Team.Members: expected 1 generic parameters, but the property type has 2

I feel I am doing something retarded here, having so many problems trying to map a simple IDictionary<string, string>. Using 1.0.0.593. Help!

  1. Support Staff 2 Posted by James Gregory on 03 Aug, 2010 11:29 PM

    James Gregory's Avatar

    As a user of our Dictionary support, you might be interested in our rework of this feature: Dictionary mapping redesign. We're working on simplifying the syntax, while making it more flexible at the same time.

    You should just be able to do the following with the new design:

    HasMany(x => x.Members)
      .Index("Name")
      .Element("ProjectRole")
      .Cascade.All();

    Excluding the Index and Element calls would result in the columns being Key and Value respectively.

  2. 3 Posted by Paul Hatcher on 30 Aug, 2010 12:22 PM

    Paul Hatcher's Avatar

    Mapping a dictionary<string, string> was possible in 1.0 with the following syntax

    HasMany(x => x.Properties)

       .Table("ProductProperty")                    
       .KeyColumn("ProductId")
       .AsMap<string>(
              index => index.Column("PropertyName").Type<string>(), 
              element => element.Column("PropertyValue").Type<string>())
       .Cascade.All();

    However, this breaks under 1.1 and I can't find a working example - is this supported in 1.1 or do we need to wait on your new design?

  3. Support Staff 4 Posted by James Gregory on 30 Aug, 2010 03:28 PM

    James Gregory's Avatar

    Anything that worked in 1.0 should still work in 1.1. The new design is in master right now.

  4. 5 Posted by Mike Cole on 14 Oct, 2010 04:25 PM

    Mike Cole's Avatar

    Hi James,
    I am unable to map this same scenario using the reworked method you supplied. I am using version 1.1.0.685.

    In the reworked method I get the following error:
    FluentNHibernate.Mapping.OneToManyPart' does not contain a definition for 'Index' and no extension method 'Index' accepting a first argument of type 'FluentNHibernate.Mapping.OneToManyPart' could be found (are you missing a using directive or an assembly reference?)

    Am I missing something? Do I need a newer version?

  5. Support Staff 6 Posted by James Gregory on 05 Nov, 2010 02:18 PM

    James Gregory's Avatar

    Those methods are unavailable in the latest builds, that's what the "reworked" design did.

    To quote myself:

    You should be aware that the master branch (and consequently the CI builds that are available on the site) are considered unstable; it's the branch that will eventually become 2.0, but that's quite a way-away yet, and will include many breaking changes.

    If you're wanting to keep on top of the bugfixes but don't want all the breaking changes, you should follow the v1.x branch instead. Binaries aren't currently available on the website (though you can get them from our CI system), but they will be before long.

    I'm looking to update the website very soon to make all this clear, as it's becoming obvious people might not be entirely aware what they're getting into when they're taking the CI builds from the website.

  6. 7 Posted by Mike Cole on 05 Nov, 2010 02:28 PM

    Mike Cole's Avatar

    Ah, makes sense, sorry for the confusion.

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is 4 times four?