如何让 Fluent NHibernate 自动映射一个继承自接口的抽象类的类?

发布于 2024-08-12 06:28:04 字数 1163 浏览 3 评论 0原文

我刚刚开始使用 Fluent NHibernate,并在尝试自动映射我的实体时遇到了以下问题:

public interface IDataEntity {}

public abstract class PhysicalEntity : IDataEntity {

         public virtual int Id { get;  set; }
         public virtual string Name { get; set; }
}

public class Mine  : PhysicalEntity {

        public virtual string MineString { get; set; }
}

private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                .ConnectionString(c => c.FromConnectionStringWithKey("CSMID_FNH")))
                .Mappings(m =>
                m.AutoMappings.Add(
                AutoMap.AssemblyOf<Mine>()
                .Where(t => t.Namespace == "DAL.DomainModel" && t.IsClass && !t.Name.EndsWith("Attribute") )
                .IgnoreBase<PhysicalEntity>()))
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }

现在,如果我删除对 IDataEntity 接口的引用,自动映射就会起作用。我尝试在接口中插入 ID 字段,但这会导致 NHibernate 运行时错误,就像告诉自动映射忽略 IDataEntity 类型一样。我在这里缺少什么?我真的希望我的域中的所有类都继承自 IDataEntity。

I've just started using Fluent NHibernate and have run into the following problem trying to automap my entities:

public interface IDataEntity {}

public abstract class PhysicalEntity : IDataEntity {

         public virtual int Id { get;  set; }
         public virtual string Name { get; set; }
}

public class Mine  : PhysicalEntity {

        public virtual string MineString { get; set; }
}

private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                .ConnectionString(c => c.FromConnectionStringWithKey("CSMID_FNH")))
                .Mappings(m =>
                m.AutoMappings.Add(
                AutoMap.AssemblyOf<Mine>()
                .Where(t => t.Namespace == "DAL.DomainModel" && t.IsClass && !t.Name.EndsWith("Attribute") )
                .IgnoreBase<PhysicalEntity>()))
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }

Now if I remove the reference to the IDataEntity interface, the automapping works. I tried inserting an ID field in the interface however this results in a NHibernate runtime error, as does telling the auto map to ignore the IDataEntity type. What am I missing here? I'd really like all the classes in my domain to inherit from IDataEntity.

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

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

发布评论

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

评论(1

断桥再见 2024-08-19 06:28:04

好的,所以我想我有答案了。

我所要做的就是修改我的 IDataEntity,如下所示:

public interface IDataEntity
{
int Id { 得到; }

最初尝试使用 get 和 set 进行此操作,但后来我遇到了问题,因为我的抽象类使用了受保护的集并且无法从接口继承。将 setter 排除在界面之外似乎目前可行,希望它不会引入任何其他问题。

Ok, so I think I have an answer.

All I had to do was modify my IDataEntity like so:

public interface IDataEntity
{
int Id { get; }
}

I'd tried this originally with a get and set, but then I would have problems because my abstract class used a protected set and couldnt inherit from the interface. Leaving the setter out of the interface seems to be working for now, hopefully it doesnt introduce any other issues.

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