这个Fluent NHibernate配置出了什么问题?

时间:2022-06-01 03:11:53

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:

以下设置有什么问题? AutoPersistanceModel上的Where过滤器似乎不起作用,并且表名约定似乎也不起作用。我经常得到的错误是“命名空间'urn中的元素'类':nhibernate-mapping-2.2'在命名空间'urn:nhibernate-mapping-2.2'中有无效的子元素'property'。预期的可能元素列表:命名空间'urn:nhibernate-mapping-2.2'中的'meta,jcs-cache,cache,id,composite-id'。“这是我的代码:

    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())
            );
    }

2 个解决方案

#1


James is leading you correctly but his snippet is wrong.

詹姆斯正确地引导你,但他的片段是错误的。

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

Is what you're after! Replace "ID" with what ever your actual property is.

是你要追求的!将“ID”替换为您的实际财产。

#2


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.

唯一的例外是说NHibernate首先遇到了 元素,这是无效的。 NHibernate hbm文件中的第一个元素应该(几乎)始终是Id,因此AutoPersistenceModel似乎找不到您的标识符。

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.

您的实体中的命名如何命名? AutoPersistenceModel期望它们字面上称为Id,如果它们有任何不同,那么它将无法找到它们。

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.

您可以使用FindIdentity配置选项覆盖AutoPersistenceModel查找ID的方式,如果您无法修改实体,这可能很有用。

// if your Id is EntityId
.WithSetup(s =>
  s.FindIdentity = property => property.DeclaredType.Name + "Id"
)

#1


James is leading you correctly but his snippet is wrong.

詹姆斯正确地引导你,但他的片段是错误的。

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

Is what you're after! Replace "ID" with what ever your actual property is.

是你要追求的!将“ID”替换为您的实际财产。

#2


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.

唯一的例外是说NHibernate首先遇到了 元素,这是无效的。 NHibernate hbm文件中的第一个元素应该(几乎)始终是Id,因此AutoPersistenceModel似乎找不到您的标识符。

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.

您的实体中的命名如何命名? AutoPersistenceModel期望它们字面上称为Id,如果它们有任何不同,那么它将无法找到它们。

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.

您可以使用FindIdentity配置选项覆盖AutoPersistenceModel查找ID的方式,如果您无法修改实体,这可能很有用。

// if your Id is EntityId
.WithSetup(s =>
  s.FindIdentity = property => property.DeclaredType.Name + "Id"
)