c# - 自动属性的 Fluent NHibernate PropertyNotFoundException

标签 c# fluent-nhibernate automatic-properties

我正在尝试让 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 : 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'`

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

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

最佳答案

结果是:约定

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

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

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

关于c# - 自动属性的 Fluent NHibernate PropertyNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4054477/

相关文章:

javascript - 从 C# 中的列表转换为 html(嵌套 ul li)

database - 是否可以使用 NHibernate 创建数据库?

C# - 如何复制/创建对象作为后代

c# - 在 C# 中使用 if 语句检查表是否存在?

c# - 减少 Update 和 Sum Query C# Access DB 的时间?

NHibernate 集合加载, "illegal access to loading collection"

c# - Fluent NHibernate - 一对至多 (1 – 0..1) 关系

c# - .NET - 尝试编译自动属性时出错

C# 3.0 Auto-Properties - 是否可以添加自定义行为?

c# - C# 中的自动属性不会导致开销吗?