Fluent NHibernate 对象化/关联关系映射
好吧,我对 Fluent 和 NHibernate 还很陌生,我不确定如何映射数据库中存在的这种特定关系。下面的 ER 图概述了我的表结构。
下面是我当前的 C# 实体类和 Fluent 映射:
public class GroupHeader
{
public virtual Guid Id { get; private set;}
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<RightHeader> AllowedRights { get; set; }
public virtual IList<RightHeader> DeniedRights { get; set; }
public virtual IList<RightHeader> UnsetRights { get; set; }
}
public class RightHeader
{
public virtual decimal Num { get; private set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
public class GroupHeaderMap : ClassMap<GroupHeader>
{
public GroupHeaderMap()
{
Table("GROUP_HEADER");
Id(x => x.Id, "GROUP_ID");
Map(x => x.Name, "GROUP_NAME").Unique();
Map(x => x.Description, "GROUP_DESCRIPTION");
HasManyToMany(x => x.AllowedRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = 1")
.Cascade.SaveUpdate();
HasManyToMany(x => x.DeniedRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = -1")
.Cascade.SaveUpdate();
HasManyToMany(x => x.UnsetRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = 0")
.Cascade.SaveUpdate();
}
}
public class RightHeaderMap : ClassMap<RightHeader>
{
public RightHeaderMap()
{
Table("RIGHT_HEADER");
Id(x => x.Num, "RIGHT_NUM");
Map(x => x.Name, "RIGHT_NAME");
Map(x => x.Description, "RIGHT_DESCRIPTION");
}
}
目前,每当我创建新的 GroupHeader 并填充时由于 GROUP_RIGHT_COMPOSITE 中的 RIGHT_VALUE 列不允许为空,AllowedRights 成员变量无法插入这些记录。我不太确定如何以流畅的方式映射它,或者我应该在我的实体中真正处理它。任何帮助将不胜感激。
Ok I'm pretty new to Fluent and NHibernate and I'm not sure how to map this specific relationship that exists in my database. I have the following ER diagram below that outlines my table structure.
Below are my current C# entity classes and Fluent mappings:
public class GroupHeader
{
public virtual Guid Id { get; private set;}
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<RightHeader> AllowedRights { get; set; }
public virtual IList<RightHeader> DeniedRights { get; set; }
public virtual IList<RightHeader> UnsetRights { get; set; }
}
public class RightHeader
{
public virtual decimal Num { get; private set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
public class GroupHeaderMap : ClassMap<GroupHeader>
{
public GroupHeaderMap()
{
Table("GROUP_HEADER");
Id(x => x.Id, "GROUP_ID");
Map(x => x.Name, "GROUP_NAME").Unique();
Map(x => x.Description, "GROUP_DESCRIPTION");
HasManyToMany(x => x.AllowedRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = 1")
.Cascade.SaveUpdate();
HasManyToMany(x => x.DeniedRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = -1")
.Cascade.SaveUpdate();
HasManyToMany(x => x.UnsetRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = 0")
.Cascade.SaveUpdate();
}
}
public class RightHeaderMap : ClassMap<RightHeader>
{
public RightHeaderMap()
{
Table("RIGHT_HEADER");
Id(x => x.Num, "RIGHT_NUM");
Map(x => x.Name, "RIGHT_NAME");
Map(x => x.Description, "RIGHT_DESCRIPTION");
}
}
Currently whenever I create a new GroupHeader and populate the AllowedRights member variable it fails to insert these records because the RIGHT_VALUE column in GROUP_RIGHT_COMPOSITE doesn't allow nulls. I'm not really sure how to map this in fluent or where I should really handle this in my entities. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在上述映射中发现的问题是,我试图将 USER_GROUP_COMPOSITE 表视为关系而不是实体本身。我修改了上面的代码,以便存在一个名为 GroupRight 的新实体,并且在该实体内引用 GroupHeader 和 RightHeader。
我认为这解决了我应该如何映射这些数据的总体问题,但我仍然在将这些数据保存到数据库时遇到问题。我修改后的问题(包括对我的实体和映射的更新)可以在此处找到 发布
我发现对解决此问题有用的文章是:
链接 1
链接 2
The problem I found with the above mapping is that I was trying to treat the USER_GROUP_COMPOSITE table as a relationship and not as an entity itself. I have modified the above code such that a new entity called GroupRight exists and has references to both GroupHeader and RightHeader inside the entity.
I think this solved my overall question of how I should map these but I am still having problems persisting this data to the database. My modified question (including updates to my entities and mappings) can be found here Post
The articles that I found useful in solving this issue were:
Link 1
Link 2