如何使用NHibernate的ManyToMany与属性(列)在联接表(流利NHibernate的) [英] How to use NHibernate ManyToMany with properties (columns) on Join Table (Fluent NHibernate)

查看:225
本文介绍了如何使用NHibernate的ManyToMany与属性(列)在联接表(流利NHibernate的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的类,我需要NHibernate很好地玩。

  public class Customer 
{
public int ID {get;组; }
public string Name {get; set;}
}

public class Product
{
public int ID {get;组; }
public string Name {get; set;}
}

public class CustomerPricing
{
public int ID {get;组; }
public decimal Price {get; set;}
public Customer Customer {get;组; }
public Product Product {get; set;}
public datetime ExpiresOn {get;组; }
public string ApprovedBy {get; set;}
}

我是使用流利的映射和HasManyToMany不适用于此(我可以告诉)。我正在通过使用一个HasMany,然后在模型中做一些LINQ查询(yuck)来解决它。



在此先感谢。



Kyle

解决方案

不知道如何在Fluent中做到这一点,在加入表格中,您需要从CustomerPricing进行多对一到客户和产品。在一个hbm.xml中,CustomerPricing的映射看起来像

 <多对一名称=产品列=ProductIDnot-null =true/> 


 < set name =CustomerPricingtable =CustomerPricinginverse =true> 
< key column =CustomerID>< / key>
< / set>


I have the following classes that I need NHibernate to play nicely with. How do I do it?

public class Customer
{
   public int ID { get; set; }
   public string Name {get;set;}
}

public class Product
{
   public int ID { get; set; }
   public string Name {get;set;}
}

public class CustomerPricing
{
   public int ID { get; set; }
   public decimal Price {get;set;}
   public Customer Customer { get; set; }
   public Product Product {get;set;}
   public datetime ExpiresOn { get; set; }
   public string ApprovedBy {get;set;}
}

I am using fluent mappings and the HasManyToMany doesn't work for this (that I can tell). I'm currently working around it by using a HasMany then doing some LINQ queries in the model (yuck).

Thanks in advance.

Kyle

解决方案

No idea how to do it in Fluent, but because you're storing data in the joining table, you'll need to go Many-to-one from CustomerPricing to both Customer and Product. In an hbm.xml, the mapping for CustomerPricing would look like

<many-to-one name="Product" column="ProductID" not-null="true" />
<many-to-one name="Customer" column="CustomerID" not-null="true" />

Then in your Customer class (or in both, if desired) you'd add:

<set name="CustomerPricing" table="CustomerPricing" inverse="true">
        <key column="CustomerID"></key>
        <one-to-many class="CustomerPricing" />
</set>

这篇关于如何使用NHibernate的ManyToMany与属性(列)在联接表(流利NHibernate的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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