NHibernate 3.2 Mapping IDictionary By Code

对着背影说爱祢 提交于 2019-12-11 01:35:29

问题


I'm having a problem mapping to IDictionary using the new Loquacious configuration.

Here's the class:

public class Person
{
    public Person()
    {
        Description = new Dictionary<int, string>();
    }

    public virtual int Id { get; set; }

    // can be in various languages
    public virtual IDictionary<int, string> Resources { get; set; }
}

public class PersonResource
{
    public virtual string Description { get; set; }
}

Here's the mapping:

public class TestPersonMap : ClassMapping<TestPerson>
{
    Table("TestPersons");

    Id(c => c.Id, m => m.Generator(Generators.HighLow, gm => gm.Params(new { max_low = 1000 })));

    Map(c => c.Resources, mpm =>
                                {
                        mpm.Table("TestPersonResources");
                        mpm.Key(km => km.Column("Id"));
                     },
            mkr => mkr.Component(cem => cem.Property(p => p.Description)));

This produces a table in the database like this:

TestPersons
-----------
Id

TestPersonResources
-------------------
Id
Description
idx

The question is, how do I change the name of the 'idx' column in the TestPersonResources table to Lcid?

I tried looking at this example http://code.google.com/p/codeconform/source/browse/ConfOrm/ConfOrm.UsageExamples/ComponentAsDictionaryKey/Demo.cs

But I can't seem to apply it to my problem.

Thanks in advance!


回答1:


After messing around and looking harder at the NHibernate source code, I think I finally got it working. Here's what I did:

Map(c => c.Resources, mpm =>
                            {
                    mpm.Key(km => km.Column("Id"));
                    mpm.Table("TestPersonResources");
                },
            mkr => mkr.Element(mkm => mkm.Column("Lcid")),
            cer => cer.Component(cem => cem.Property(p => p.Description, pm => pm.Length(100))));


来源:https://stackoverflow.com/questions/7354483/nhibernate-3-2-mapping-idictionary-by-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!