4

I have a LanguageMap object which contains a dictionary to map a language code to it's value. This object is used in many any object that needs to have multiple languages. Therefore it is referenced throughout my data model.

 public class LanguageMap
{
    public virtual Guid Id { get; set; }
    public virtual IDictionary<String, String> Map { get; set; }

    public LanguageMap()
    {
        Map = new Dictionary<string, string>();
    }
}

My mapping is as follows:

public class LanguageMapMapping : ClassMap<LanguageMap>
{
    public LanguageMapMapping()
    {
        Id(x => x.Id);

        HasMany(x => x.Map)
            .Table("LanguageMapMap")
            .AsMap<string>("LanguageKey")
            .Element("value")
            .Cascade.All().Cascade.AllDeleteOrphan();

    }
}

On all the classes that have a LanguageMap property (e.g. public virtual LanguageMap Details{ get; set; }), the mapping for that property is:

References(x => x.Details)

For some reason this always creates an extra table in the database: enter image description here

The LanguageMap table is unnecessary and just adds extra joins for no reason.

Any idea how to fix the mappings?

Rafi
  • 2,433
  • 1
  • 25
  • 33
  • @(Diego Mijelshon) do you have any ideas? – Rafi Jan 11 '16 at 09:12
  • Try this solution: http://stackoverflow.com/a/6408785/818088 – dcidral Jan 13 '16 at 10:45
  • @dcidral How would that solution help? That is talking about column type. My question is about removing an extra table. Can you explain? – Rafi Jan 13 '16 at 10:51
  • How many rows expected in the Map property? – Max Kilovatiy Jan 13 '16 at 12:06
  • @rafi Your Id columns is a Guid. That post explain how to map a Guid primary key column. Maybe the extra table is created to deal with this problem since you didn't specify in your mapping class how to properly map your Id property (auto generated Guid or database generated Guid or system generated Guid). I'm not sure that is your problem but may worth a try. – dcidral Jan 13 '16 at 13:06
  • @MaxKvt Not exactly sure, how many but let's say between 10 and 100 depending on how many languages we end up supporting. Why do you ask? – Rafi Jan 13 '16 at 14:09
  • @dcidral That didn't make a difference. Tried it. Thanks anyway – Rafi Jan 13 '16 at 14:15
  • @rafi Sorry, I missed something while reading your post. Everthing is how it should be. I think what you are trying to do is to reference Dictionary by other class. The LanguageMap shouldn't exist, you should map the Dictionary at the class you are referencing LanguageMap. Try to use the HasMany mapping on those classes instead. – dcidral Jan 13 '16 at 16:27
  • @dcidral I'll try that. Post an answer and if it works I'll give you the bounty but it expires soon. – Rafi Jan 14 '16 at 06:55
  • @dcidral I tried that. It won't work if you have two properties in the same class have the dictionary and have the mapping create the dictionary in the same table because of duplicate keys created. – Rafi Jan 14 '16 at 08:06
  • @rafi I'm sorry to hear that. I think it is inevitable to have this aditional table, it is necessary otherwise your Languages table would need to have a foreign key column to each table that needs the language. low-flying-pelican is correct. – dcidral Jan 14 '16 at 11:55

1 Answers1

3

NHibernate creates the additional table because it needs an Id.

Without the table LanguageMap NHibernate would not be able to provide a unique key to map. Imagine you have 2 tables User and Role needs to be linked to the LanguageMapMap table, and both can have Id=1 entity. If both of these entities needs entries in LanguageMapMap table NHibernate would not be able to put it together.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • In my situation I'm using guid so there isn't an issue of conflicting keys. My question isn't simply why does nhibernate do this but what is the proper way to create the mapping to remove the unnecessary table. Any ideas? – Rafi Jan 10 '16 at 19:18