Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you map an entity -> interface relationship using Fluent NHibernate?

given the following class definition:

public class Order {
  public IProduct Product {get;set;}
}

I have this (fluent) mapping

References(x=>x.Product, "ProductId");

And get this exception: An association from the table Orders refers to an unmapped class, which makes sense because it doesn't know what implementation I will pass to it.

I understand why I have to define the type in the mapping (IProduct could be anything) but I'm not sure how to do it.

Thanks,

Kyle

like image 642
Kyle West Avatar asked Jun 20 '09 18:06

Kyle West


2 Answers

I think what you're looking for is .References<Product>(x=>x.Product, "ProductId");

Incidentally the same is true for .HasMany<>

This seems to do the same as <... class="Product" /> in xml

I wouldn't recommend mapping to the interface as it breaks the whole point of using one - you run into problems as soon as it starts implementing IStorable and NH can't cope with the multiple inheritance.

like image 94
Stu Avatar answered Nov 15 '22 11:11

Stu


Try mapping the interface IProduct instead of the concrete class Product. (Note, I'm not talking about mapping the Product field of class Order.)

like image 32
yfeldblum Avatar answered Nov 15 '22 09:11

yfeldblum