如何覆盖复合 Id 的自动映射,与流畅的 nhibernate 一对多关系?
我正在轮询多个系统(域)以获取安全信息,因此我正在处理域用户及其角色。我的实体设置如下所示,但我在 AutoMapper 覆盖中设置 domainUser.HasMany 关系时遇到问题。
您会注意到我没有domainUser.DomainUserId和role.RoleId,这使得这变得更加简单(没有compositeIds)。我已经避免了这些字段,因为我已经有了一个自然的复合键,并且它将在以下情况下填充:我从下游域提取这些数据。如果我添加这些人工键,我必须在调用 session.Merge(domainUser) 之前预取它们的值。我正在努力避免这样做。
实体对象是显而易见的(我希望),但这就是我所得到的。
public class DomainUser
{
public virtual int Domain_Id { get; set; }
public virtual string DomainUserLogin { get; set; }
public virtual string EmployeeId { get; set; }
// extra field removed for breviety
public DomainUser()
{
this.Roles = new List<DomainUserRole>();
}
public virtual void AddRole(DomainUserRole role)
{
role.DomainUser = this;
this.Roles.Add(role);
}
// overrides for equals and getHashCode
}
我的数据库
public class DomainUserRole
{
public virtual DomainUser DomainUser { get; set; }
public virtual string DataSegment { get; set; } // Some group of data a user has access to, like US or China
public virtual string RoleName { get; set; }
public virtual string RoleDescription { get; set; }
// extra field removed for breviety
// overrides for equals and getHashCode
}
模式非常简单。
替代文本 http://lh6.ggpht.com/ _MV6QGBD11JE/S3iX2qcP_jI/AAAAAAAAEE0/PGIO07BlCSo/s800/Untitled.gif.jpg
我已经像这样启动了 IAutoMappingOverride 类。但是,我不知道如何为角色设置 hasMany 。它一直告诉我
NHibernate.FKUnmatchingColumnsException:
Foreign key (FK20531BE4163641BB:tblDomainUserRoles [DomainUser]))
must have same number of columns as the referenced primary key
(tblDomainUsers [Domain_Id, DomainUserLogin]).
如何设置外键以使用这两个字段?
public class DomainUserMap : IAutoMappingOverride<DomainUser>
{
public void Override(AutoMapping<DomainUser> mapping)
{
mapping.CompositeId()
.KeyProperty(user => user.Domain_Id, "Domain_Id")
.KeyProperty(user => user.DomainUserLogin, "DomainUserLogin");
// I"ve tried this.
// mapping.HasMany(x => x.Roles)
// .KeyColumns.Add("Domain_Id")
// .KeyColumns.Add("DomainUserLogin");
// also tried this where I define this FK in DB with both fields.
// mapping.HasMany(x => x.Roles)
// .ForeignKeyConstraintName("FK_tblDomainUserRoles_tblDomainUsers")
}
}
public class DomainUserRoleMap : IAutoMappingOverride<DomainUserRole>
{
public void Override(AutoMapping<DomainUserRole> mapping)
{
mapping.CompositeId()
.KeyReference(role => role.DomainUser)
.KeyProperty(role => role.DataSegment)
.KeyProperty(role => role.RoleName);
}
}
I'm polling mulitple systems (domains) for security info so I'm dealing domainUsers and their roles. I've got my entities setup as show below, but I'm having trouble setting up the domainUser.HasMany relationship in the AutoMapper override.
You'll notice that I don't have domainUser.DomainUserId and role.RoleId which make this much more simple (no compositeIds.) I've avoided those fields because I've already got a natural composite key and it will be populate when I pull this data from the downstream domain. If I add those artificial keys, I'll have to pre-fetch their values before I call session.Merge(domainUser). I'm trying to avoid doing that.
The entity objects are obvious (I hope) but here is what I've got.
public class DomainUser
{
public virtual int Domain_Id { get; set; }
public virtual string DomainUserLogin { get; set; }
public virtual string EmployeeId { get; set; }
// extra field removed for breviety
public DomainUser()
{
this.Roles = new List<DomainUserRole>();
}
public virtual void AddRole(DomainUserRole role)
{
role.DomainUser = this;
this.Roles.Add(role);
}
// overrides for equals and getHashCode
}
and
public class DomainUserRole
{
public virtual DomainUser DomainUser { get; set; }
public virtual string DataSegment { get; set; } // Some group of data a user has access to, like US or China
public virtual string RoleName { get; set; }
public virtual string RoleDescription { get; set; }
// extra field removed for breviety
// overrides for equals and getHashCode
}
My db schema is pretty simple.
alt text http://lh6.ggpht.com/_MV6QGBD11JE/S3iX2qcP_jI/AAAAAAAAEE0/PGIO07BlCSo/s800/Untitled.gif.jpg
I've got the IAutoMappingOverride classes started like this. But, I'm at a loss of how to setup the hasMany for roles. It keeps giving me
NHibernate.FKUnmatchingColumnsException:
Foreign key (FK20531BE4163641BB:tblDomainUserRoles [DomainUser]))
must have same number of columns as the referenced primary key
(tblDomainUsers [Domain_Id, DomainUserLogin]).
How do I setup that foreign key to use both those fields?
public class DomainUserMap : IAutoMappingOverride<DomainUser>
{
public void Override(AutoMapping<DomainUser> mapping)
{
mapping.CompositeId()
.KeyProperty(user => user.Domain_Id, "Domain_Id")
.KeyProperty(user => user.DomainUserLogin, "DomainUserLogin");
// I"ve tried this.
// mapping.HasMany(x => x.Roles)
// .KeyColumns.Add("Domain_Id")
// .KeyColumns.Add("DomainUserLogin");
// also tried this where I define this FK in DB with both fields.
// mapping.HasMany(x => x.Roles)
// .ForeignKeyConstraintName("FK_tblDomainUserRoles_tblDomainUsers")
}
}
public class DomainUserRoleMap : IAutoMappingOverride<DomainUserRole>
{
public void Override(AutoMapping<DomainUserRole> mapping)
{
mapping.CompositeId()
.KeyReference(role => role.DomainUser)
.KeyProperty(role => role.DataSegment)
.KeyProperty(role => role.RoleName);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终手动编辑了 hbm 文件。看起来最新的 Fluent Nhibernate 版本解决了这个问题。这是我的 hbm 文件的样子。
这是角色的文件。
I did endup hand editing the hbm files. It looks like the most recent build on fluent Nhibernate addresses this issue. Here is what my hbm file look like.
here is the file for roles.