Auto 属性的 Fluent NHibernate PropertyNotFoundException

发布于 2024-09-29 18:49:18 字数 2247 浏览 8 评论 0原文

我正在尝试让 Fluent NHibernate 为我映射一个集合。我的类定义如下:

public abstract class Team 
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
}

public class ClientTeam : Team
{
    public virtual IEnumerable<Client> Clients { get; set; }
}

public class Client
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Identifiers { get; set; }
}

我的映射:

public class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        Table("Team");
        Id(x => x.Id).GeneratedBy.Assigned();
        Map(t => t.TeamName);
    }
}

public class ClientTeamMap : SubclassMap<ClientTeam>
{
    public ClientTeamMap()
    {
        HasMany(t => t.Clients);
    }
}

public class ClientMap : ClassMap<Client>
{
    public ClientMap()
    {
        Table("Client");
        Id(c => c.Id);
        Map(c => c.Name);
        Map(c => c.Identifiers);
    }
}

我构建了一个单元测试,实例化一个团队,然后尝试保留它(测试库中有依赖项配置等):

public class TeamMapTester : DataTestBase
{
    [Test]
    public void Should_persist_and_reload_team()
    {
        var team = new ClientTeamDetail
        {
            Id = Guid.NewGuid(),
            TeamName = "Team Rocket",
            Clients = new[] 
            {
                new ClientDetail {ClientName = "Client1", ClientIdentifiers = "1,2,3"}
            }
        };

        using (ISession session = GetSession())
        {
            session.SaveOrUpdate(team);
            session.Flush();
        }

        AssertObjectWasPersisted(team);
    }
}

当我运行测试时,我得到这个错误:

SetUp:FluentNHibernate.Cfg.FluentConfigurationException:创建 SessionFactory 时使用了无效或不完整的配置。检查 PotentialReasons 集合和 InnerException 了解更多详细信息。

  • 未通过数据库方法配置数据库。

----> NHibernate.MappingException:无法编译映射文档:(XmlDocument)
----> NHibernate.PropertyNotFoundException:在“ClientTeam”类中找不到字段“_clients”`

我已经浏览了 NHibernate 文档并进行了一些谷歌搜索,但我找不到任何似乎可以解决此问题的内容。 Fluent NHibernate 的引用方法的文档明确使用自动属性,所以我确信这不是问题。

为什么 NHibernate 会认为 _clients 是在这种情况下应该映射的字段?

I'm trying to get Fluent NHibernate to map a collection for me. My class definitions are as follows:

public abstract class Team 
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
}

public class ClientTeam : Team
{
    public virtual IEnumerable<Client> Clients { get; set; }
}

public class Client
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Identifiers { get; set; }
}

My mappings:

public class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        Table("Team");
        Id(x => x.Id).GeneratedBy.Assigned();
        Map(t => t.TeamName);
    }
}

public class ClientTeamMap : SubclassMap<ClientTeam>
{
    public ClientTeamMap()
    {
        HasMany(t => t.Clients);
    }
}

public class ClientMap : ClassMap<Client>
{
    public ClientMap()
    {
        Table("Client");
        Id(c => c.Id);
        Map(c => c.Name);
        Map(c => c.Identifiers);
    }
}

I've built a unit test that instantiates a team and then attempts to persist it (the test base has dependency configuration, etc. in it):

public class TeamMapTester : DataTestBase
{
    [Test]
    public void Should_persist_and_reload_team()
    {
        var team = new ClientTeamDetail
        {
            Id = Guid.NewGuid(),
            TeamName = "Team Rocket",
            Clients = new[] 
            {
                new ClientDetail {ClientName = "Client1", ClientIdentifiers = "1,2,3"}
            }
        };

        using (ISession session = GetSession())
        {
            session.SaveOrUpdate(team);
            session.Flush();
        }

        AssertObjectWasPersisted(team);
    }
}

When I run the test, I get this error:

SetUp : FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  • Database was not configured through Database method.

----> NHibernate.MappingException: Could not compile the mapping document: (XmlDocument)
----> NHibernate.PropertyNotFoundException : Could not find field '_clients' in class 'ClientTeam'`

I've looked through the NHibernate documentation and done some google searching, but I can't find anything that appears to address this issue. The documentation for Fluent NHibernate's Referencing methods explicitly uses auto properties, so I'm sure that's not the issue.

Why might NHibernate think that _clients is the field it should map in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-10-06 18:49:18

原因是:约定

Fluent 映射被设置为尝试通过需要支持字段来强制执行只读集合属性。有问题的ICollectionConvention

public class CollectionAccessConvention : ICollectionConvention
{
    public void Apply(ICollectionInstance instance)
    {
        instance.Fetch.Join();
        instance.Not.LazyLoad();
        instance.Access.CamelCaseField(CamelCasePrefix.Underscore);
    }
}

它要求集合支持字段采用驼峰式命名并以下划线开头。

And the reason turns out to be: Conventions.

The Fluent mappings were set up to try to enforce read-only collection properties, by requiring a backing field. The ICollectionConvention in question:

public class CollectionAccessConvention : ICollectionConvention
{
    public void Apply(ICollectionInstance instance)
    {
        instance.Fetch.Join();
        instance.Not.LazyLoad();
        instance.Access.CamelCaseField(CamelCasePrefix.Underscore);
    }
}

which requires that collection backing fields be camelCased and start with an underscore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文