3

I'm dealing with a legacy database that has a locked schema. The problem I'm facing is that a number of the tables are keyed off known/fixed/hard-coded entity type Id values instead of having a column values. This means that I can't use the normal References construct.

For the tables with the ENTITY_TYPEID I can do this:

public class EntityMap : ClassMap<Entity>
{
   public EntityMap()
   {
      References(x => x.Type)
         .Columns("ENTITY_SECTION","ENTITY_TYPEID");
   }
}

which happily populates the Entity.Type.

For the tables of a fixed/known/hard-coded type I need to map to a hard-coded value instead of the ENTITY_TYPE column, so to para-phrase in code:

public class EntityXMap : ClassMap<EntityX>
{
   public EntityXMap(int entityType)
   {
      References(x => x.Type)
         .Columns("ENTITY_SECTION", "ENTITY_TYPE = 123" );
   }
}

HasMany() has a Where() construct that I could use in that case...

Any ideas how to achieve something similar here?

svick
  • 236,525
  • 50
  • 385
  • 514
SteveH
  • 343
  • 2
  • 10

1 Answers1

0

maybe overkill but you could try

// add to config
var typemap = new TypeMap();
typemap.Id(x => x.Section, "ENTITY_SECTION");
typemap.Where("ENTITY_TYPE = 123");
typemap.EntityName("Type for EntityX");

References(x => x.Type)
   .Column("ENTITY_SECTION")
   .EntityName("Type for EntityX");
Firo
  • 30,626
  • 4
  • 55
  • 94
  • Thanks for that Firo. Unfortunately, I can't change the Type objects Primary Key as it's used in the full object - I guess I over simplified the question some what. I've managed to get a work around by creating a SQL view that adds the fixed value for the ENTITY_TYPE column and using that view as the table in FNH. I'm not sure that option will fly in production... Any alternatives? – SteveH Jun 11 '12 at 16:43
  • views are often the easier solution, use them if you can. In my experience locked down schema often means no extra views, in that case there are only workarounds left. One Alternative i could think of would be a formula, not sure if it is applicable for compositeIds – Firo Jun 12 '12 at 05:42
  • Having done a little more testing this seems to be an omission in Fluent NHibernate (and NHibernate’s Mapping by code, for that matter). If I drop back down to an XML configuration I can do this using a many-to-one element with a formula instead of the ENTITY_TYPE column - both Fluent and Mapping by code complain about column counts not matching. – SteveH Jun 13 '12 at 13:40
  • because both generate a default column which disable the formula – Firo Jun 13 '12 at 19:08
  • Fair enough - thanks for your help Firo, I appreciate it. Do you think it's worth raising as an issue in the relevant support forums? – SteveH Jun 14 '12 at 08:13
  • i think it's worth it because at least in FNH the code change should be minimal so there might be someone fixing this. However it is a seldom used feature therefor it could take long until someone fixes this – Firo Jun 14 '12 at 11:42