复合 ID 的自动映射不起作用

发布于 2024-11-08 16:48:16 字数 2032 浏览 2 评论 0原文

我正在使用 Sharp-Architecture 框架,并且有一个如下所示的实体:

public class BaanAlternateItemKey : ValueObject
{
    public virtual string ItemId { get; protected set; }

    public virtual string AlternateItemId { get; protected set; }
}

public class BaanAlternateItem : EntityWithTypedId<BaanAlternateItemKey>, IAlternateItem
{
    #region IAlternateItem Members

    public virtual IItem Item { get; protected set; }

    public virtual int Priority { get; protected set; }

    public virtual DateTime ExpirationDate { get; protected set; }

    #endregion
}

我有一个如下所示的自动映射覆盖:

public class BaanAlternateItemAutoMappingOverride : IAutoMappingOverride<BaanAlternateItem>
{
    public void Override(AutoMapping<BaanAlternateItem> mapping)
    {
        mapping.ReadOnly();

        mapping.Table("VIEW_BAAN_ALTERNATE_ITEMS");

        mapping.CompositeId<BaanAlternateItemKey>(x => x.Id)
            .KeyProperty(x => x.ItemId, "ITEM_ID")
            .KeyProperty(x => x.AlternateItemId, "ALT_ITEM_ID");

        mapping.References<BaanItem>(x => x.Item, "ALT_ITEM_ID");
    }
}

我收到此异常:

----> NHibernate.MappingException:无法确定以下类型:iPFS.Core.Baan.BaanAlternateItemKey,iPFS.Core,Version = 0.0.4154.21888,Culture =中性,PublicKeyToken = null,对于列:NHibernate.Mapping.Column(Id)

我没有知道我做错了什么。有什么建议吗?

更新:如果我使用流畅的映射来映射它,它可以正常工作:

public class BaanAlternateItemMap : ClassMap<BaanAlternateItem>
{
    public BaanAlternateItemMap()
    {
        ReadOnly();

        Table("VIEW_BAAN_ALTERNATE_ITEMS");

        CompositeId<BaanAlternateItemKey>(x => x.Id)
            .KeyProperty(x => x.ItemId, "ITEM_ID")
            .KeyProperty(x => x.AlternateItemId, "ALT_ITEM_ID");

        Map(x => x.Priority, "PRIORITY");
        Map(x => x.ExpirationDate, "EXPIRATION_DATE").CustomType("Timestamp");

        References<BaanItem>(x => x.Item, "ALT_ITEM_ID");
    }
}

I'm using the Sharp-Architecture framework and I have an entity that looks like this:

public class BaanAlternateItemKey : ValueObject
{
    public virtual string ItemId { get; protected set; }

    public virtual string AlternateItemId { get; protected set; }
}

public class BaanAlternateItem : EntityWithTypedId<BaanAlternateItemKey>, IAlternateItem
{
    #region IAlternateItem Members

    public virtual IItem Item { get; protected set; }

    public virtual int Priority { get; protected set; }

    public virtual DateTime ExpirationDate { get; protected set; }

    #endregion
}

I have an auto mapping override that looks like this:

public class BaanAlternateItemAutoMappingOverride : IAutoMappingOverride<BaanAlternateItem>
{
    public void Override(AutoMapping<BaanAlternateItem> mapping)
    {
        mapping.ReadOnly();

        mapping.Table("VIEW_BAAN_ALTERNATE_ITEMS");

        mapping.CompositeId<BaanAlternateItemKey>(x => x.Id)
            .KeyProperty(x => x.ItemId, "ITEM_ID")
            .KeyProperty(x => x.AlternateItemId, "ALT_ITEM_ID");

        mapping.References<BaanItem>(x => x.Item, "ALT_ITEM_ID");
    }
}

I'm getting this exception:

----> NHibernate.MappingException : Could not determine type for: iPFS.Core.Baan.BaanAlternateItemKey, iPFS.Core, Version=0.0.4154.21888, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Id)

I have no idea what I'm doing wrong. Any suggestions?

UPDATE: If I map this using fluent mapping it works fine:

public class BaanAlternateItemMap : ClassMap<BaanAlternateItem>
{
    public BaanAlternateItemMap()
    {
        ReadOnly();

        Table("VIEW_BAAN_ALTERNATE_ITEMS");

        CompositeId<BaanAlternateItemKey>(x => x.Id)
            .KeyProperty(x => x.ItemId, "ITEM_ID")
            .KeyProperty(x => x.AlternateItemId, "ALT_ITEM_ID");

        Map(x => x.Priority, "PRIORITY");
        Map(x => x.ExpirationDate, "EXPIRATION_DATE").CustomType("Timestamp");

        References<BaanItem>(x => x.Item, "ALT_ITEM_ID");
    }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身骄傲 2024-11-15 16:48:16

这不应该是这样吗?:

public class BaanAlternateItemAutoMappingOverride : IAutoMappingOverride<BaanAlternateItem>
{
public void Override(AutoMapping<BaanAlternateItem> mapping)
{
    mapping.ReadOnly();

    mapping.Table("VIEW_BAAN_ALTERNATE_ITEMS");

    mapping.CompositeId<BaanAlternateItem>(x => x.Id) //are you mapping BaanAlternateItem right?
        .KeyProperty(x => x.ItemId, "ITEM_ID")
        .KeyProperty(x => x.AlternateItemId, "ALT_ITEM_ID");

    mapping.References<BaanItem>(x => x.Item, "ALT_ITEM_ID");
}
}

此错误表明映射引擎不知道 BaanAlternateItemKey 类型的任何映射。因此,请确保您有地图。

Is that shouldn't be that?:

public class BaanAlternateItemAutoMappingOverride : IAutoMappingOverride<BaanAlternateItem>
{
public void Override(AutoMapping<BaanAlternateItem> mapping)
{
    mapping.ReadOnly();

    mapping.Table("VIEW_BAAN_ALTERNATE_ITEMS");

    mapping.CompositeId<BaanAlternateItem>(x => x.Id) //are you mapping BaanAlternateItem right?
        .KeyProperty(x => x.ItemId, "ITEM_ID")
        .KeyProperty(x => x.AlternateItemId, "ALT_ITEM_ID");

    mapping.References<BaanItem>(x => x.Item, "ALT_ITEM_ID");
}
}

This error says that the mapping engine have no idea about any map for type BaanAlternateItemKey. So make sure that you have map for it.

蝶…霜飞 2024-11-15 16:48:16

我相信您需要将 AlternateItemId 的 KeyProperty 映射转换为 KeyReference,然后删除引用映射,如下所示。这假设该 Item 也已被映射,并且它具有指定的与 ALT_ITEM_ID 值匹配的 Id 属性。

public class BaanAlternateItemAutoMappingOverride : IAutoMappingOverride<BaanAlternateItem>
{
    public void Override(AutoMapping<BaanAlternateItem> mapping)
    {
        mapping.ReadOnly();

        mapping.Table("VIEW_BAAN_ALTERNATE_ITEMS");

        mapping.CompositeId<BaanAlternateItem>(x => x.Id) //are you mapping BaanAlternateItem right?
            .KeyProperty(x => x.ItemId, "ITEM_ID")
            .KeyReference(x => x.Item, "ALT_ITEM_ID");
    }
}

I believe you'll want to convert the KeyProperty mapping for AlternateItemId to a KeyReference and then remove the References mapping as shown below. This assumes the Item has been mapped also and it has an Id property designated that matches the ALT_ITEM_ID value.

public class BaanAlternateItemAutoMappingOverride : IAutoMappingOverride<BaanAlternateItem>
{
    public void Override(AutoMapping<BaanAlternateItem> mapping)
    {
        mapping.ReadOnly();

        mapping.Table("VIEW_BAAN_ALTERNATE_ITEMS");

        mapping.CompositeId<BaanAlternateItem>(x => x.Id) //are you mapping BaanAlternateItem right?
            .KeyProperty(x => x.ItemId, "ITEM_ID")
            .KeyReference(x => x.Item, "ALT_ITEM_ID");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文