views:

1678

answers:

2

What's wrong with the following setup? The Where filter on the AutoPersistanceModel does not appear to be working and the table name convention does not appear to be working either. The error I'm evenually getting is "The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'property' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, jcs-cache, cache, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'." Here's my code:

    public ISessionFactory BuildSessionFactory()
    {
        return Fluently.Configure()
            .Database(
                OracleConfiguration.Oracle9.ConnectionString(
                c => c.FromConnectionStringWithKey("ConnectionString")))
            .Mappings(m =>
                          {
                              m.AutoMappings.Add(GetAutoPersistanceModel);
                              m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly());
                          })
            .BuildSessionFactory();
    }

    public AutoPersistenceModel GetAutoPersistanceModel()
    {
        return AutoPersistenceModel.MapEntitiesFromAssemblyOf<User>()
            .Where(type => type.IsClass && !type.IsAbstract && type.Namespace == "Some.Namespace")
            .ConventionDiscovery.Add<IConvention>(
                Table.Is(x => "tbl" + x.EntityType.Name.Pluralize())
            );
    }
+6  A: 

The exception is saying NHibernate has encountered a <property /> element first, which is invalid. The first element in an NHibernate hbm file should (nearly) always be an Id, so it seems the AutoPersistenceModel isn't finding your identifiers.

How are your Ids named in your entities? The AutoPersistenceModel expects them to be literally called Id, if they're anything different then it won't find them.

You can use the FindIdentity configuration option to override how the AutoPersistenceModel finds Ids, which can be useful if you're unable to modify your entities.

// if your Id is EntityId
.WithSetup(s =>
  s.FindIdentity = property => property.DeclaredType.Name + "Id"
)
James Gregory
+1  A: 

James is leading you correctly but his snippet is wrong.

.WithSetup(s=> s.FindIdentity = p => p.Name == "ID"));

Is what your after! Replace "ID" with what ever your actual property is!

bleevo

related questions