流利的Nhibernate多对多的映射方式 [英] Fluent Nhibernate Many to Many Mapping Way

查看:104
本文介绍了流利的Nhibernate多对多的映射方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类Order and Items

我想要一个像这样的方法

  class Order 
{
public virtual IList< Item> GetItems(Order order)
{
//获取该订单的项目。
}
}
class Item
{
公共虚拟IList< Order> GetOrders(商品)
{
//获取商品所有的订单。






$ b是写这个方法创建一个方法我应该创建一个属性

  public virtual IList< Item>项目{get;组; } 

我应该怎么做这个是nhibernate的映射?

$ b $显然你有一个多对多的关系:一个订单可以有很多项目,一个项目可以属于很多订单。在一个关系数据库中,你需要用一个单独的表来表示这个表,我假设你有这个表 - 我们假设这个表被称为 OrdersItems



遵循 Fluent NHibernate文档中的Store / Product示例 a>您可以在 Order Orders 中创建 Items $ c $> code $> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ {
公共虚拟IList< Item>项目{get;保护组}
}

class Item
{
公共虚拟IList< Order>订单{get;保护组}

$ / code>

映射:

  public class OrderMap:ClassMap< Order> 
{
public OrderMap()
{
HasManyToMany(x => x.Items)
.Cascade.All()
.Table( OrdersItems);
}
}

public class ItemMap:ClassMap< Item>
{
public ItemMap()
{
HasManyToMany(x => x.Orders)
.Cascade.All()
.Inverse()
.Table(OrdersItems);
}
}


I have two classes Order and Items

I want a method like this

class Order
{
    public virtual IList<Item> GetItems(Order order)
    {
         //get items for that order.
    }
}
class Item
{
    public virtual IList<Order> GetOrders(Item item)
    {
         //get all the orders in which that items is present.
    }
}

Is it write to create a method like this or instead should I create a property

     public virtual IList<Item> Items { get; set; }

And how should I do the mapping for this is nhibernate??

解决方案

Apparently you have a many-to-many relationship: An order can have many items and an item can belong to many orders. In a relational database you need to express this with a separate table which I presume you have - let's assume this table is called OrdersItems.

Following the Store/Product example from the Fluent NHibernate documentation you would create an Items property in an Order and an Orders property in Item:

class Order
{
    public virtual IList<Item> Items { get; protected set; }
}

class Item
{
    public virtual IList<Order> Orders { get; protected set; }
}

And the mappings:

public class OrderMap : ClassMap<Order>
{
    public OrderMap()
    {
        HasManyToMany(x => x.Items)
           .Cascade.All()
           .Table("OrdersItems");
    }
}

public class ItemMap : ClassMap<Item>
{
    public ItemMap()
    {
        HasManyToMany(x => x.Orders)
           .Cascade.All()
           .Inverse()
           .Table("OrdersItems");
    }
}

这篇关于流利的Nhibernate多对多的映射方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆