How to use NHibernate ManyToMany with properties (columns) in Join table (Fluent NHibernate)

I have the following classes in which I need NHibernate to play well. How to 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 use quick mappings, and HasManyToMany does not work for this (what can I say). I am currently working on this using HasMany, then doing some LINQ queries in the model (yuck).

Thanks in advance.

Kyle

+1
source share
2 answers

, Fluent, , CustomerPricing . 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>
+4

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

Customer

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

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

Source: https://habr.com/ru/post/1771512/


All Articles