使用 Fluent NHibernate 映射接口的实现列表

发布于 2024-11-26 06:16:04 字数 2122 浏览 1 评论 0原文

使用以下映射时出现以下错误:

NHibernate.MappingException:关联引用未映射的类:GeoTriggers.Domain.Entities.ITrigger

实体:

public class Profile
{
    public virtual int Id { get; set; }
    public virtual string Password { get; set; }
    public virtual string Username { get; set; }
    public virtual IList<ITrigger> Triggers { get; set; }        
}

public interface ITrigger
{
    int Id { get; set; }
    decimal Latitude { get; set; }
    decimal Longitude { get; set; }
    decimal Radius { get; set; }
}

public class EmailTrigger : ITrigger
{
    public virtual string RecipientAddress { get; set; }
    public virtual string SenderAddress { get; set; }
    public virtual string Subject { get; set; }
    public virtual string Body { get; set; }

    #region ITrigger Members

    public virtual int Id { get; set; }
    public virtual decimal Latitude { get; set; }
    public virtual decimal Longitude { get; set; }
    public virtual decimal Radius { get; set; }

    #endregion
}

使用以下类映射:

public sealed class ProfileMap : ClassMap<Profile>
{
    public ProfileMap()
    {
        HasMany(x => x.Triggers).Cascade.All();
    }
}

public sealed class TriggerClassMap : ClassMap<ITrigger>
{
    public TriggerClassMap()
    {            
    }    
}

public sealed class EmailTriggerClassMap : SubclassMap<EmailTrigger>
{
    public EmailTriggerClassMap()
    {            
    }
}

使用以下 FluentNHibernate 配置:

FluentConfiguration fluent = Fluently.Configure(_configuration)
                .Mappings(
                    x => x.AutoMappings.Add(
                        AutoMap.Assemblies(typeof (TEntity).Assembly).Where(
                            assembly => assembly.Namespace.Contains("Entities"))));

所有我的实体位于包含单词“Entities”的命名空间中。我这样做是为了避免让我的所有实体都从一个共同的基础继承。

我试图将配置保持在最低限度,因此我选择允许 Fluent NHibernate 按照其默认约定自由运行。通常,我什至没有类映射文件。但是,现在,由于我试图映射实现公共接口的对象列表,它变得很粘。

该错误表明关联(我假设 ProfileMap 中的一对多)引用了未映射的类 (ITrigger)。根据文档,听起来我需要的只是基/接口的类映射和实现的子类映射。我有那个。我缺少什么?

I get the following error when using the below mappings:

NHibernate.MappingException: Association references unmapped class: GeoTriggers.Domain.Entities.ITrigger

Entities:

public class Profile
{
    public virtual int Id { get; set; }
    public virtual string Password { get; set; }
    public virtual string Username { get; set; }
    public virtual IList<ITrigger> Triggers { get; set; }        
}

public interface ITrigger
{
    int Id { get; set; }
    decimal Latitude { get; set; }
    decimal Longitude { get; set; }
    decimal Radius { get; set; }
}

public class EmailTrigger : ITrigger
{
    public virtual string RecipientAddress { get; set; }
    public virtual string SenderAddress { get; set; }
    public virtual string Subject { get; set; }
    public virtual string Body { get; set; }

    #region ITrigger Members

    public virtual int Id { get; set; }
    public virtual decimal Latitude { get; set; }
    public virtual decimal Longitude { get; set; }
    public virtual decimal Radius { get; set; }

    #endregion
}

Using the following class maps:

public sealed class ProfileMap : ClassMap<Profile>
{
    public ProfileMap()
    {
        HasMany(x => x.Triggers).Cascade.All();
    }
}

public sealed class TriggerClassMap : ClassMap<ITrigger>
{
    public TriggerClassMap()
    {            
    }    
}

public sealed class EmailTriggerClassMap : SubclassMap<EmailTrigger>
{
    public EmailTriggerClassMap()
    {            
    }
}

Using the following FluentNHibernate configuration:

FluentConfiguration fluent = Fluently.Configure(_configuration)
                .Mappings(
                    x => x.AutoMappings.Add(
                        AutoMap.Assemblies(typeof (TEntity).Assembly).Where(
                            assembly => assembly.Namespace.Contains("Entities"))));

All my entities are in a namespace that includes the word "Entities". I did this to avoid having to make all my entities inherit from a common base.

I am trying to keep my configuration to a minimum, so I have opted to allow Fluent NHibernate to run free with its default conventions. Normally, I don't even have class map files. But, now, since I'm trying to map a list of objects that implement a common interface, it has gotten sticky.

The error says that an association (I assume the one-to-many in the ProfileMap) references an unmapped class (ITrigger). According to the docs, it sounds like all I need to have is a class map for the base/interface and subclass maps for the implementations. I have that. What am I missing?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文