Fluent NHibernate: do not generate any mapping for class during SchemaExport

1k views Asked by At

I've got an application talking to two databases, and it runs fine, and my tests pass, except when it tries to export the current DB schema, it outputs everything for both DBs in one block, and when it tries to validate the object model / DB schema line up, it tries to look in one database for everything, when one class (which is external data, hence the foreign DB) should not be mapped. I provided a mapping override for the class so that NH can correctly load/use data from the foreign DB (it's read-only), but now when I export my schema, it tries to make that table.

I tried IgnoreBase(typeof(Unit)) but this has no effect (whereas IgnoreBase(typeof(Entity)) does work correctly). I have decorated the custom repository methods with [SessionFactory(DataGlobals.FOREIGN_DB_FACTORY_KEY)] where that simply defines a constant string to use as a key for the SessionFactory, but I'm sure if there's something I need to decorate the class (Unit) with or pass a different parameter to SchemaExport ...

public void CanGenerateDatabaseSchema(){ 
var session = NHibernateSession.GetDefaultSessionFactory().OpenSession();

        using (TextWriter stringWriter = new StreamWriter("../../../../db/schema/UnitTestGeneratedSchema.sql"))
        {
            new SchemaExport(configuration).Execute(true, false, false, session.Connection, stringWriter);
        }
}

public void CanConfirmDatabaseMatchesMappings()
    {
        var allClassMetadata = NHibernateSession.GetDefaultSessionFactory().GetAllClassMetadata();

        foreach (var entry in allClassMetadata)
        {
            NHibernateSession.Current.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco))
                 .SetMaxResults(0).List();
        }
    }
1

There are 1 answers

7
Cole W On

In Fluent NHibernate you can use SchemaAction.None to disable the generation of the schema for a particular table.

Example:

public class BankInfoMap : ClassMap<BankInfo>
{
    public BankInfoMap()
    {
        Schema(“viplookups.dbo”);
        Table(“bnkroute”);
        SchemaAction.None();

        Id(x => x.Id).Column(“bnkrouteid”);
        Map(x => x.AbaNumber).Column(“crouting”);
        Map(x => x.Name).Column(“ccompname”);
        Map(x => x.City).Column(“ccity”);
        Map(x => x.State).Column(“cstate”);
        Map(x => x.PhoneNumber).Column(“cphone1″);
    }
}

Taken from: http://lostechies.com/rodpaddock/2010/06/29/using-fluent-nhibernate-with-legacy-databases/