0

I am using the following fluent mapping for two classes :

1.Application class :

  public ThirdPartyAppMap()
    {
        Schema("Data");
        Id(x => x.ThirdPartyAppId).GeneratedBy.Identity();
        Map(x => x.AppName);
        Map(x => x.CreationTime);
        Map(x => x.Enabled);
        References(x => x.ThirdPartyCustomer, "ThirdPartyCustomerId").Class(typeof(ThirdPartyCustomer));
        HasMany(x => x.ThirdPartyUsersIds).Table("Data.ThirdPartyUser").KeyColumn("ThirdPartyAppId").Element("UserId").AsBag();
    }

Which is a simple application in my core domain

and a sub class of it : ApplicationExt mapping :

 public ThirdPartyAppProxExtMap()
    {
        Table("Data.ThirdPartyApp");
        KeyColumn("ThirdPartyAppId");
        this.HasManyIds(x => x.DeviceToAppRelIds, "Data.ThirdPartyDeviceToAppRel", "AppId", "ThirdPartyDeviceToAppRelId");
    }

Which is mapped to the same table but adds a hasmany property which is a list of ids . This class is in a higher level domain which is based on the core domain , the core domain does not need to know the rels of the application class while the higher domain does.

The mapping works fine but I encounter an exception while working with a NH session object :

When I call

Session.Refresh(BaseAppObject)

I receive the following exception :

Unable to cast object of type 'RedPill.Applicative.Module.CM.Domain.ThirdPartyApp' to type 'RedPill.Applicative.Module.PI.Domain.ThirdPartyAppProxExt'.

It throws invalidcastexception because it tried to assign data to the extended type property which does not exists on the BaseAppObject , only on the ExtAppObject . Looking at the profiler the sesion object tried to fetch ExtApplicationObject instead of BaseApplicationObject. So basically , I did Session.Get() , recieved BaseAppObject which works fine , but when I am trying to refresh it using Session.Refresh() it tries to Fetch ExtApplicationObject and when NH tries to build the object from the selection it throws invalid cast exception.

Any help on this one ?

Regards ,

James

James Roeiter
  • 861
  • 1
  • 9
  • 23
  • Shouldn't it be mapped with a discriminator as opposed to a subclass? I thought subclass mappings had to have their own table with whatever extension properties you want. – Fourth Mar 13 '12 at 19:47
  • 1
    @Fourth http://www.philliphaydon.com/2011/08/fluent-nhibernate-table-inheritance-discriminators/ , no , in my case I want to create a class not based on a value in a column but based on the generic T type im asking for from the session where different T has different mappings eventhough they are both mapped to the same table. – James Roeiter Mar 13 '12 at 22:49
  • Ok , I do not know if this is the best solution but what I did was to change my DB model so instead of one table that two entities are mapped to it , where one entity derives the other , is that I split the table into two tables that one derive the other and now its a simple Fluent NHibernate inheritance -- perhaps its better modeling or perhaps sometimes you simply need to adapt yourself to the tools you are working with ... – James Roeiter Mar 13 '12 at 23:08

1 Answers1

0

you tried to use table-per-class in a single table which fails for many reasons. Either change to multiple tables or table-per-hierarchy with discriminator

Firo
  • 30,626
  • 4
  • 55
  • 94