具有通用基类的 NHibernate 映射

发布于 2024-11-05 21:15:21 字数 2173 浏览 0 评论 0原文

我正在使用 Fluent NHibernate 来映射我的实体,并且我已经找到了一组正在映射的探针。我尝试了 SubclassMap 的不同变体,但似乎无法正确映射购物车、项目或订单。 Item、CartItem、OrderItem 和 ProjectItem 映射良好。我对 NHibernate 相当陌生,我只是不知道如何告诉它做我想做的事。

编辑:这是我要使用此继承结构的架构:在此处输入图像描述

public abstract class Item
{
    public virtual int Id {get;set;}
    public virtual decimal Price {get;set;}
}

public class CartItem : Item
{
    public virtual string ProjectName {get;set;}
}

public class OrderItem : Item
{
    public virtual IList<Payment> Payments {get;set;}
}

public class ProjectItem : Item
{
    public virtual string ProjectName {get;set;}
}

public abstract class ItemCollection<T> where T: Item
{
    public virutal IList<T> Items {get;set;}
}

public abstract class CommerceCollection<T> : ItemCollection<T> where T : Item
{
    public virtual decimal Total {get;set;}
}

public class Project : ItemCollection<ProjectItem> 
{
    public virtual string ProjectName {get;set;}
}

public class Cart : CommerceCollection<CartItem> {}

public class Order : CommerceCollection<OrderItem>
{
    public virtual string OrderTrackingNumber {get;set;}
}

/*-------  MAPS  -------*/
public class ItemMap: ClassMap<Item>
{
    public ItemMap()
    {
        MapId();
        Map(x=> x.Price);
    }
}

public class OrderItemMap : SubclassMap<OrderItem>
{
    public OrderItemMap()
    {
        Map(x => x.OrderItemProperty);
    }
}

public class CartItemMap : SubclassMap<CartItem>
{
    public CartItemMap()
    {
        Map(x => x.CartItemProperty);
    }
}

public class ProjectItemMap : SubclassMap<ProjectItem>
{
    public ProjectItemMap()
    {
        Map(x => x.ProjectItemProperty);
    }
}

 public class CartMap : ClassMap<Cart>
    {
        public CartMap()
        {
            Map(x => x.Total);
            HasMany(x => x.Items);
        }
    } 

    public class OrderMap : ClassMap<Order>
    {
        public OrderMap()
        {
            Map(x => x.Total);
            HasMany(x => x.Items);
        }
    }

I'm using Fluent NHibernate to map my entities and I've come to a set that I'm having probelsm gettting mapped. I've tried different variations of SubclassMap but I can't seem to get Cart, Project or Order mapped correctly. Item, CartItem, OrderItem and ProjectItem map fine. I'm fairly new to NHibernate and I just can't figure out how to tell it to do what I want.

EDIT: This is the schema I'm going for with this inheritance structure:enter image description here

public abstract class Item
{
    public virtual int Id {get;set;}
    public virtual decimal Price {get;set;}
}

public class CartItem : Item
{
    public virtual string ProjectName {get;set;}
}

public class OrderItem : Item
{
    public virtual IList<Payment> Payments {get;set;}
}

public class ProjectItem : Item
{
    public virtual string ProjectName {get;set;}
}

public abstract class ItemCollection<T> where T: Item
{
    public virutal IList<T> Items {get;set;}
}

public abstract class CommerceCollection<T> : ItemCollection<T> where T : Item
{
    public virtual decimal Total {get;set;}
}

public class Project : ItemCollection<ProjectItem> 
{
    public virtual string ProjectName {get;set;}
}

public class Cart : CommerceCollection<CartItem> {}

public class Order : CommerceCollection<OrderItem>
{
    public virtual string OrderTrackingNumber {get;set;}
}

/*-------  MAPS  -------*/
public class ItemMap: ClassMap<Item>
{
    public ItemMap()
    {
        MapId();
        Map(x=> x.Price);
    }
}

public class OrderItemMap : SubclassMap<OrderItem>
{
    public OrderItemMap()
    {
        Map(x => x.OrderItemProperty);
    }
}

public class CartItemMap : SubclassMap<CartItem>
{
    public CartItemMap()
    {
        Map(x => x.CartItemProperty);
    }
}

public class ProjectItemMap : SubclassMap<ProjectItem>
{
    public ProjectItemMap()
    {
        Map(x => x.ProjectItemProperty);
    }
}

 public class CartMap : ClassMap<Cart>
    {
        public CartMap()
        {
            Map(x => x.Total);
            HasMany(x => x.Items);
        }
    } 

    public class OrderMap : ClassMap<Order>
    {
        public OrderMap()
        {
            Map(x => x.Total);
            HasMany(x => x.Items);
        }
    }

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

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

发布评论

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

评论(2

浸婚纱 2024-11-12 21:15:21

这是您的映射的简化版本吗?如果不是:

  1. 您的 SubclassMappings 缺少对 KeyColumn("Item_id"); 的调用(这是获取每个子类的表映射所必需的)。
  2. 您的 HasMany 调用缺少像 .KeyColumn("Cart_id") 这样的调用。

Is this a simplified version of your mapping? If not:

  1. Your SubclassMappings are missing a call to KeyColumn("Item_id"); (this is needed to get a table-per-subclass mapping).
  2. Your HasMany calls are missing a call like .KeyColumn("Cart_id").
ぃ弥猫深巷。 2024-11-12 21:15:21

我明白了我需要做什么。对于实现通用基类 (CommerceCollection) 的类,我必须为通用集合添加一个映射及其参数,然后在子类上指定 KeyColumn 属性。

这是新的 CartMap:

public class CartCollectionMap : CommerceCollectionMap<CartItem> {}

    public class CartMap: SubclassMap<Cart>
    {
        public CartMap()
        {
            KeyColumn("CommerceCollection_id");
        }
    } 

Nhibernate 正在生成如下所示的 SQL,该 SQL 会在列名中的 ' 上失败。

CREATE TABLE Cart (CommerceCollection'1_id int PRIMARY KEY)

I figured out what I needed to do. For the classes that implemented the generic base class (CommerceCollection) I had to add a map for the generic collection with it's argument and then specify the KeyColumn property on the subclass.

Here is the new CartMap:

public class CartCollectionMap : CommerceCollectionMap<CartItem> {}

    public class CartMap: SubclassMap<Cart>
    {
        public CartMap()
        {
            KeyColumn("CommerceCollection_id");
        }
    } 

Nhibernate was generating SQL like below which would fail on the ' in the column name.

CREATE TABLE Cart (CommerceCollection'1_id int PRIMARY KEY)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文