如何让 Fluent NHibernate 自动映射一个继承自接口的抽象类的类?
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我想我有答案了。
我所要做的就是修改我的 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.