How to map multiple interface implementation using NHibernate
I searched a lot about how to map multiple inheritance or multiple interface implantation using EntityFramework or NHibernate But I didn't find anything useful.
I Simply want to map this structure using NHibernate:
public interface IA
{
string A { get; set; }
}
public interface IB
{
string B { get; set; }
}
public class C : IA, IB
{
string A { get; set; }
string B { get; set; }
}
As far as i know mapping this structure to a relational database means just to have foreign keys related with the interfaces primary keys, therefore the interfaces should have Keys like these:
public interface IA
{
Guid AId { get; set; }
string A { get; set; }
}
public interface IB
{
Guid BId { get; set; }
string B { get; set; }
}
public class C : IA, IB
{
public virtual Guid AId { get; set; }
public virtual Guid BId { get; set; }
public virtual string A { get; set; }
public virtual string B { get; set; }
}
But how to map this structure using NHibernate Or EntityFramework,and I don't know why multiple interface mappin开发者_运维技巧g is not mentioned in their documentation!
In NHibernate, you'll just map C as if the interfaces didn't exist.
You'll still be able to query on the interfaces, thanks to implicit polymorphism.
You will map it as any other class because this is not inheritance mapping. Moreover your code cannot be compiled because you must implement all properties in class C
so you will get:
public interface IA
{
Guid AId { get; set; }
string A { get; set; }
}
public interface IB
{
Guid BId { get; set; }
string A { get; set; }
}
public class C : IA, IB
{
public virtual Guid AId { get; set; }
public virtual Guid BId { get; set; }
public virtual string A { get; set; }
}
Now your code can be compiled and you have class as any other. You will map AId and BId as composite key (depending on used ORM) and you are done. This is not inheritance because you have just single entity and no base enity. At least this is how it works with Entity framework.
As what I've founded it's not possible to have multiple inheritance in a relational database due to the concept and what Diego said is true in a "not interfaces get persisted scenario".
精彩评论