Nhibernate 具有父类的类有许多项-通过代码映射是不可能的
我对nHibernate有一个问题:如果一个实体继承自一个基本实体,那么这个子实体就不能被另一个实体引用Nhibernate 具有父类的类有许多项-通过代码映射是不可能的,nhibernate,inheritance,nhibernate-mapping,mapping-by-code,Nhibernate,Inheritance,Nhibernate Mapping,Mapping By Code,我对nHibernate有一个问题:如果一个实体继承自一个基本实体,那么这个子实体就不能被另一个实体引用 我的数据库中有一个继承层次结构,如下所示: VideoFeed是一种VisualFeed VideoFeed有许多playlassignment 下面是SQL的(意译)定义 create table VisualFeed ( id BIGINT NOT NULL -- Primary key ) create table VideoFeed ( id BIGINT N
我的数据库中有一个继承层次结构,如下所示:
是一种VideoFeed
VisualFeed
有许多VideoFeed
playlassignment
create table VisualFeed
(
id BIGINT NOT NULL -- Primary key
)
create table VideoFeed
(
id BIGINT NOT NULL -- Primary key, foriegn key references VisualFeed
)
create table PlaylistAssignment
(
id BIGINT NOT NULL, -- Primary key
VideoFeed_Id BIGINT NOT NULL -- Foreign key to VideoFeed
)
以及类定义
public class VisualFeed
{
public virtual long? Id { get; set; }
}
public class VideoFeed : VisualFeed
{
public virtual ISet<PlaylistAssignment> PlaylistAssignments { get; set; }
}
public class PlaylistAssignment
{
public virtual long? Id { get; set; }
public virtual VideoFeed VideoFeed { get; set; }
}
以下是VideoFeed
的映射代码:
public static void Map(ModelMapper mapper)
{
mapper.JoinedSubclass<DATMedia.CMS.EntityLibrary.Entities.VideoFeed>(
joinedSubClassMapper =>
{
joinedSubClassMapper.Table("cms_VideoFeed");
joinedSubClassMapper.Key(keyMapper =>
{
keyMapper.Column("Id");
}
);
joinedSubClassMapper.Set(
playerGroup => playerGroup.PlaylistAssignments,
setPropertiesMapper =>
{
setPropertiesMapper.Key(
keyMapper =>
{
keyMapper.Column("VideoFeed_Id");
keyMapper.PropertyRef(videoFeed => videoFeed.Id);
}
);
setPropertiesMapper.Cascade(Cascade.All | Cascade.DeleteOrphans);
setPropertiesMapper.Inverse(true);
setPropertiesMapper.OrderBy(playlistAssignment => playlistAssignment.AssignmentRank);
},
collectionElementRelation =>
{
collectionElementRelation.OneToMany();
}
);
}
);
}
ownerEntityType
等于“VideoFeed”,而ReflectedType
和DeclaringType
都等于“VisualFeed”(父类名称)。即使这一切都是正确的,仍会抛出ArgumentOutOfRangeException
有人能想出一个解决办法吗
稍后编辑此问题是由对
setPropertiesMapper
的显式引用调用引起的。事实上,这是在一个不同的儿童班上,我从这个问题中提出了这个问题(试图将问题简单化,这是错误的尝试)
真正的罪魁祸首是以下“cms_VisualFeedAssignment”的映射:
mapper.Class<DATMedia.CMS.EntityLibrary.Entities.VisualFeed>(
classMapper =>
{
classMapper.Table("cms_VisualFeed");
classMapper.Id(
visualFeed => visualFeed.Id,
idMapper =>
{
idMapper.Column("Id");
idMapper.Generator(Generators.HighLow,
generatorMapper =>
{
generatorMapper.Params(
new
{
max_lo = 256,
column = "NextHi",
where = "TableName='VisualFeed'"
}
);
}
);
}
);
classMapper.Set<DATMedia.CMS.EntityLibrary.Entities.VisualFeedAssignment>(
visualFeed => visualFeed.Assignments,
bagPropertiesMapper =>
{
bagPropertiesMapper.Inverse(true);
bagPropertiesMapper.Lazy(CollectionLazy.Lazy);
bagPropertiesMapper.Key(
keyMapper =>
{
keyMapper.Column("VisualFeed_Id");
//keyMapper.PropertyRef<long?>(visualFeed => visualFeed.Id);
}
);
bagPropertiesMapper.Table("cms_VisualFeedAssignment");
},
collectionElementRelation =>
{
collectionElementRelation.OneToMany();
}
);
}
);
}
mapper.Class(
类映射器=>
{
类映射表(“cms_VisualFeed”);
classMapper.Id(
visualFeed=>visualFeed.Id,
idMapper=>
{
idMapper.列(“Id”);
idMapper.Generator(Generators.HighLow,
generatorMapper=>
{
generatorMapper.Params(
新的
{
最大值=256,
column=“NextHi”,
其中=“TableName='VisualFeed'”
}
);
}
);
}
);
类映射器.Set(
visualFeed=>visualFeed.Assignments,
bagPropertiesMapper=>
{
bagPropertiesMapper.Reverse(真);
bagPropertiesMapper.Lazy(CollectionLazy.Lazy);
bagPropertiesMapper.钥匙(
键映射器=>
{
keyMapper.列(“VisualFeed_Id”);
//keyMapper.PropertyRef(visualFeed=>visualFeed.Id);
}
);
bagPropertiesMapper.表格(“cms_视觉反馈分配”);
},
CollectionElementralation=>
{
collectionelementralation.OneToMany();
}
);
}
);
}
当我注释掉
PropertyRef
调用时,它成功了。您不需要这行:
keyMapper.PropertyRef(videoFeed => videoFeed.Id);
将自动引用
VideoFeed.Id
。尝试删除该行,它应该会起作用。谢谢您的建议。映射现在完成,但在代码的另一部分中引发异常。我已用新错误更新了我的问题。您的代码中还有其他问题。你能提供播放列表分配的映射吗?你在运行什么版本的NH?好的,我已经添加了播放分配映射。我现在正在使用nHibernate 3.3.1。我本来打算升级到3.3.2,但似乎您只能下载二进制文件,而不能下载代码包。(你知道代码是否已经打包到任何站点的zip文件中了吗?)NH3.3.2源代码已打开,我在NH3.3.1和3.3.2上的映射没有任何异常。我在这里发布了一个测试项目:。你在这里发布的基本结构运行良好。其他地方也有失败的地方。尝试将您的映射剥离到您在此处发布的裸结构中,并以增量方式添加对象上的其他映射,直到找到不起作用的映射为止。
public void PropertyRef(MemberInfo property)
{
if (property == null)
{
mapping.propertyref = null;
return;
}
if (!ownerEntityType.Equals(property.DeclaringType) && !ownerEntityType.Equals(property.ReflectedType))
{
throw new ArgumentOutOfRangeException("property", "Can't reference a property of another entity.");
}
mapping.propertyref = property.Name;
}
mapper.Class<DATMedia.CMS.EntityLibrary.Entities.VisualFeed>(
classMapper =>
{
classMapper.Table("cms_VisualFeed");
classMapper.Id(
visualFeed => visualFeed.Id,
idMapper =>
{
idMapper.Column("Id");
idMapper.Generator(Generators.HighLow,
generatorMapper =>
{
generatorMapper.Params(
new
{
max_lo = 256,
column = "NextHi",
where = "TableName='VisualFeed'"
}
);
}
);
}
);
classMapper.Set<DATMedia.CMS.EntityLibrary.Entities.VisualFeedAssignment>(
visualFeed => visualFeed.Assignments,
bagPropertiesMapper =>
{
bagPropertiesMapper.Inverse(true);
bagPropertiesMapper.Lazy(CollectionLazy.Lazy);
bagPropertiesMapper.Key(
keyMapper =>
{
keyMapper.Column("VisualFeed_Id");
//keyMapper.PropertyRef<long?>(visualFeed => visualFeed.Id);
}
);
bagPropertiesMapper.Table("cms_VisualFeedAssignment");
},
collectionElementRelation =>
{
collectionElementRelation.OneToMany();
}
);
}
);
}
keyMapper.PropertyRef(videoFeed => videoFeed.Id);