Question

This one has me tearing my hair out. I am using the automapping features in Fluent NHibernate but it seems to be selectively ignoring some overrides but implementing others fine.

Not working:

  public class CupTree
  {
    public virtual int Id { get; set; }
    public virtual int TournamentId { get; set; }
    public virtual int CurrentRound { get; set; }
    public virtual IList<CupRound> Rounds { get; set; }
  }

Not working Override:

  public class CupTreeOverride : IAutoMappingOverride<CupTree>
  {
    public void Override(FluentNHibernate.Automapping.AutoMapping<CupTree> mapping)
    {
      mapping.HasMany(x => x.Rounds).Cascade.All();
    }
  }

Schema Generation:

  public void GenerateSchema()
    {
      var config = new MappingConfiguration();

      Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(/*my conn str*/))
         .Mappings(m => m.AutoMappings.Add(
           AutoMap.AssemblyOf<CupTree>(config)
           .Conventions.Add(
              DefaultLazy.Never(),
              Table.Is(x => x.EntityType.Name + "s"))
           .UseOverridesFromAssembly(Assembly.GetAssembly(typeof(CupTreeOverride)))
           ))
         .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))
         .BuildSessionFactory();
    } 

So, While it creates the CupTree table it does not create the CupRounds Table at all. But it does create the PreMatchTeamInfo table and all its child tables. I'm obviously doing something incredibly stupid but can't seem to spot it. Any help here greatly appreciated.

Was it helpful?

Solution

I have managed to track this down to column names. I changed the SchemaUpdate to make it print out the sql statements and ran them.

This generated SQL errors which did not cause my schema update to fail (said all was OK). I had used the name "Order" as a property name on my class, which is of course a reserved word in SQL. I changed the name to something else and all works fine.

For the next person, the way to troubleshoot this is :

  1. Make FNH generate the hbm.xml files. You do this by calling ExportTo on your AutoMap Configuration:

    .Mappings(m => m.AutoMappings.Add(
           AutoMap.AssemblyOf<CupTree>(config)
           .Conventions.Add(
              DefaultLazy.Never(),
              Table.Is(x => x.EntityType.Name + "s"))
           .UseOverridesFromAssembly(Assembly.GetAssembly(typeof(CupTreeOverride)))
           ).ExportTo(@"C:\nh.out"))
    
  2. Check the outputted files and make sure everything is correct. If not, you have a mapping problem.

  3. If your xml files are good, check the SQL statements it runs. You do this by setting the first parameter (script) on SchemaUpdate.Execute to true.

    .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
    
  4. Copy and paste the generated SQL and run it against the database. If there are any problems, you'll get errors and can take it from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top