Fluent NHibernate HasManyToMany() IDictionary<>复合ID问题

发布于 2024-10-17 05:27:43 字数 2369 浏览 3 评论 0 原文

我使用的是 Fluent NHibernate 1.1.1.694,它使用的语法与 FNH1.0 略有不同,特别是在字典映射方面。

在我的模型中,我有员工、地址和地址类型(邮政、物理等)。

public class Employee
{
    // <snip other properties />
    public virtual IDictionary Addresses { get; set; }

    public Employee()
    {
        this.Addresses = new Dictionary();
    }
}

public enum AddressType
{
    Physical,
    Postal
}

public class Address
{
    public virtual int Id { get; private set; }
    public virtual IList Lines { get; set; }
    public virtual string City { get; set; }
    public virtual string Zip { get; set; }
}

我的映射

public class EmployeeMap : ClassMap
{
    public EmployeeMap()
    {
        CompositeId()
            .KeyProperty(e => e.IdentityType)
            .KeyProperty(e => e.IdentityValue);

        // <snip other properties />

        HasManyToMany(e => e.Addresses)                
            .Inverse();
    }
}

生成以下 .hbm.xml 片段:

<map inverse="true" name="Addresses" table="EmployeeAddresses" mutable="true">
    <key>
        <column name="Employee_id" />
    </key>
    <index type="Domain.Entities.AddressType, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="Key" />
    </index>
    <many-to-many class="Domain.Entities.Address, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="Address_id" />
    </many-to-many>
</map>

喜欢生成的 .hbm.xml

<map inverse="true" name="Addresses" table="EmployeeAddresses" mutable="true">
    <key>
        <column name="IdentityType" />
        <column name="IdentityValue" />
    </key>
    <index type="Domain.Entities.AddressType, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="Key" />
    </index>
    <many-to-many class="Domain.Entities.Address, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="Address_id" />
    </many-to-many>
</map>

显然这会失败,因为我没有告诉它有一个复合 ID 可用于父密钥。但我找不到告诉它的方法。 .HasManyToMany() 不存在 .Columns 和 .ParentKeyColumns

我应该如何映射它?

I'm using Fluent NHibernate 1.1.1.694 which uses a slightly different syntax than FNH1.0, especially when it comes to dictionary mapping.

In my model, I have Employees, Addresses and AddressTypes (postal, physical, etc).

public class Employee
{
    // <snip other properties />
    public virtual IDictionary Addresses { get; set; }

    public Employee()
    {
        this.Addresses = new Dictionary();
    }
}

public enum AddressType
{
    Physical,
    Postal
}

public class Address
{
    public virtual int Id { get; private set; }
    public virtual IList Lines { get; set; }
    public virtual string City { get; set; }
    public virtual string Zip { get; set; }
}

My mappings:

public class EmployeeMap : ClassMap
{
    public EmployeeMap()
    {
        CompositeId()
            .KeyProperty(e => e.IdentityType)
            .KeyProperty(e => e.IdentityValue);

        // <snip other properties />

        HasManyToMany(e => e.Addresses)                
            .Inverse();
    }
}

The following .hbm.xml snippet is generated:

<map inverse="true" name="Addresses" table="EmployeeAddresses" mutable="true">
    <key>
        <column name="Employee_id" />
    </key>
    <index type="Domain.Entities.AddressType, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="Key" />
    </index>
    <many-to-many class="Domain.Entities.Address, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="Address_id" />
    </many-to-many>
</map>

The .hbm.xml that I'd like to generate is

<map inverse="true" name="Addresses" table="EmployeeAddresses" mutable="true">
    <key>
        <column name="IdentityType" />
        <column name="IdentityValue" />
    </key>
    <index type="Domain.Entities.AddressType, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="Key" />
    </index>
    <many-to-many class="Domain.Entities.Address, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="Address_id" />
    </many-to-many>
</map>

Obviously this will fail, because nowhere am I telling it that there is a composite ID to use for the parent key. But I can't find the methods to tell it. The .Columns and .ParentKeyColumns don't exist for .HasManyToMany<Key,Value>()

How should I map this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文