views:

18

answers:

1

Hi,

I'm having some trouble with Fluent nHibernate. I added a column to a table and I thought I'd correctly changed the mappings and the connected data objects to correctly reflect this. However when I tried to run my application again I kept getting this error:

System.Data.SqlClient.SqlException: Invalid column name 'Workflow_id'.

I really couldn't see what the problem was with the changes I'd made so I reverted back to the original versions of the mapping and data object files from source control and removed the offending column from the database. But I'm still getting the same error.

I'd like some advice on how to debug this. The SQL that gets reported on the error is semi-nonsensical:

SELECT regions0_.Page_id as Page5_1_, regions0_.Id as Id1_, regions0_.Id as Id27_0_, regions0_.RegionId as RegionId27_0_, regions0_.RegionTemplate_id as RegionTe3_27_0_, regions0_.Workflow_id as Workflow4_27_0_ FROM [Region] regions0_ WHERE regions0_.Page_id=?

And it won't execute as valid SQL anyway.

Any ideas as to where to go from here?

+1  A: 

I think you do not need to debug FluentNhibernate. The issue could be in you conventions.

As I understand you have an object Region and it is referenced to other object Workflow. So set a convention for all reference link e.q.:

        private  Action<IConventionFinder> GetConventions() 
        {
            return c =>
                { 
                    c.Add<PrimaryKeyConvention>();
                    c.Add<ReferenceConvention>();
                    c.Add<HasManyConvention>();
                    c.Add<TableNameConvention>();
                    c.Add<PropertyNameConvention>();
                };
        }

Use this private method into your implementation of the

 public AutoPersistenceModel Generate()

And the reference convention should be something like:

using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;

public class ReferenceConvention : IReferenceConvention
{
    public void Apply(IManyToOneInstance instance)
    {
        instance.Column(Inflector.Net.Inflector.Camelize(instance.Property.Name) + "Id");
    }
}

Also check that you override the mappings, if is the case.

I have an unittest witch export the mappings. Unfortunately bellow is an old version:

[Test, Ignore("Run this test only if you want to see mappings")]
    public void ShouldExportMappings()
    {
        const string mappingPath = @"mappings";

        if (!Directory.Exists(mappingPath))
            Directory.CreateDirectory(mappingPath);

        var sessionFactory = Fluently.Configure()
            .Database(SQLiteConfiguration.Standard.InMemory)
            .Mappings(m =>
                          {
                              m.FluentMappings
                                  .AddFromAssemblyOf<User>()
                                  .ExportTo(mappingPath);

                              m.AutoMappings
                                  .Add(new AutoPersistenceModelGenerator().Generate())
                                  .ExportTo(mappingPath);
                          }).BuildSessionFactory();
    } 

And finally if you really want to debug, copy the FluentNHibernate sources from their storage and include it into your sln. But this is not good idea as the problem is in your code rather in their. This will not help, you only will loose time.

isuruceanu
Thanks for your response - there's plenty of useful stuff in there.However it looks as though the root of the problem is a lot simpler - it seems that nHibernate caches a lot of stuff on the web server, which I didn't know - I assumed it rebuilt all its underlying structure each time. Stop/starting the server instance fixed the problem with no code changes.
Matt Thrower
We also have an unittest which create the database on CI server before running the unitests, an other for deploy which create a database with dummy data for manual testing.Also have other unittest witch tell us if database matches the mappings
isuruceanu