如何在连接表上使用带有属性(列)的 NHibernate ManyToMany (Fluent NHibernate)

发布于 2024-09-27 08:55:07 字数 630 浏览 2 评论 0原文

我有以下类需要 NHibernate 才能很好地使用。我该怎么做?

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;}
}

我正在使用流畅的映射,而 HasManyToMany 对此不起作用(我可以告诉)。我目前正在通过使用 HasMany 然后在模型中执行一些 LINQ 查询来解决这个问题(恶心)。

提前致谢。

凯尔

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

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

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

发布评论

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

评论(2

原来分手还会想你 2024-10-04 08:55:07

不知道如何在 Fluent 中执行此操作,但因为您将数据存储在连接表中,所以您需要从 CustomerPricing 到 Customer 和 Product 进行多对一。在 hbm.xml 中,CustomerPricing 的映射看起来像这样

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

,然后在您的 Customer 类(或两者中,如果需要)中添加:

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

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>
棒棒糖 2024-10-04 08:55:07

尝试

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public IList<Product> ProductsOwned { get; set; }
}

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public IList<Customer> Owners { get; set; }
}

使用客户映射为

 HasManyToMany<Product>(x => x.ProductsOwned) 
.WithTableName("CustomerPricing") 
.WithParentKeyColumn("CustomerID") 
.WithChildKeyColumn("ProductID")

和产品映射为

 HasManyToMany<Customer>(x => x.Owners) 
.WithTableName("CustomerPricing") 
.WithParentKeyColumn("ProductID") 
.WithChildKeyColumn("CustomerID")

Try this

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public IList<Product> ProductsOwned { get; set; }
}

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public IList<Customer> Owners { get; set; }
}

with Customer mapping as

 HasManyToMany<Product>(x => x.ProductsOwned) 
.WithTableName("CustomerPricing") 
.WithParentKeyColumn("CustomerID") 
.WithChildKeyColumn("ProductID")

and Product mapping as

 HasManyToMany<Customer>(x => x.Owners) 
.WithTableName("CustomerPricing") 
.WithParentKeyColumn("ProductID") 
.WithChildKeyColumn("CustomerID")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文