views:

27

answers:

1

Problem is simple I have two classes mapped with fluent nhibernate :

public class A: EntityBase {}
public class B: EntityBase
{
   public virtual A A_Something {get;set;}
}

with EntityBase class providing only Key property. Now i want to map them and configure my db. So here are mappings

public class AMap : DomainEntityBase<A>
{
  public AMap(){}
}

public class BMap : DomainEntityBase<B>
{
  public BMap()
  {
    References(p=>p.A_Something).Column("A_ID");
  }
}

and db configuration

Fluently.Configure().Database(...).Mappings(m =>
  m.FluentMappings.Add<BMap>()).
   ExposeConfiguration(BuildSchema).BuildSessionFactory();

Configuration is done only with BMap as unfortunately table A already exists inside database and it's data can't be changed (it's because this application is using parts of bigger database and those parts are not to be modified). So this schema generates standard exception

An association from the table [B] refers to an unmapped class: A

Is there any possibility somehow specify inside my configuration that A already exists in the db? If not what are your suggestions to solve this issue in a gentle way? Of course I can prepare *.sql files to manually create table B but this is not a solution (just pushing away problem as it will surely come back during further development). Also creating separate db and importing data from the first one could work but unfortunately there is a requirement to store data inside one (and only one) db.

A: 

You have to map all the entities you will use; whether or not their corresponding tables are present in the database is irrelevant.

Only when you are doing schema generation is it going to matter, but all you have to do is generate update scripts instead of create scripts. Given the database dialect and a connection, NHibernate will generate scripts to create missing tables and update existing tables, if necessary (which it sounds like it isn't in this case).

If the existing table's name does not match the type of the entity (the default name), you'll need to specify that in your mapping by calling:

Table("ATable");
Jay
and my question basically was how to force nhibernate to generate update scripts instead of create scripts.
tchrikch