当前位置: 动力学知识库 > 问答 > 编程问答 >

Mapping inheritance in NHibernate 3.3

问题描述:

I have the inheritance described below :

public abstract class BaseEntity<TId> {....}

public abstract class ModelEntity : BaseEntity<Int32>{....}

public abstract class AuditableEntity : ModelEntity,IAuditable{....}

public class ApplicationUser : AuditableEntity{....}

public class SuperUser : ApplicationUser

I am using NHibernate 3.3 and I want to Create the mappings for that inheritance

public abstract class ModelEntityMap<TEntity> : ClassMapping<TEntity>

where TEntity : ModelEntity

{...}

public class AuditableEntityMap<TEntity> : ModelEntityMap<TEntity> where TEntity : AuditableEntity

{ ...}

public class ApplicationUserMap : AuditableEntityMap<ApplicationUser>

{...}

public class SuperUserMap : JoinedSubclassMapping<SuperUser>{...}

When the application starts and trys to set up the database it raises the following Exception :

Ambiguous mapping for SuperUser More than one root entities was found BaseEntity / ApplicationUser

Possible solutions

-Merge the mapping of root Entity in the one is representing the real root in the hierarchy

-Inject a IModelInspector with a logic to discover the real root-entity.

I was using Fluent nhibernate with the same inheritance and worked fine with SuperUserMap defined as

public class SuperUserMap : SubClassMap {...}

I am new to Nhibernate mapping by code and quite confused !!!

网友答案:

I believe there are two ways to solve this problem: a) Using the concept of discriminator that identifies the type of the class stored and thereby the right object is retrieved from the database, in this case your class is mapped to a table that has all the columns plus the discriminator columns. Not sure how this works with multi-level inheritance but this is something that you can google.

b) take a look at this post on how he deals with inheritance: http://fabiomaulo.blogspot.co.nz/2011/04/nhibernate-32-mapping-by-code_13.html you might get some idea to solve your issue.

分享给朋友:
您可能感兴趣的文章:
随机阅读: