Fluent NHibernate:如何在 ManyToMany 上映射 where 子句过滤器
我在保留一个字段时遇到问题,该字段位于多对多关系的 were 子句中,但在类映射中没有明确存在。很难解释,但它是下面的类和映射中的“is_expred”字段。
我有以下两个类:
public class Publication
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
}
public class Role
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual IEnumerable<Publication> CurrentPublications {get; set;}
public virtual IEnumerable<Publication> ExpiredPublications {get; set;}
}
其中出版物可以存在于 CurrentPublications 和 ExpiredPublications 中的一个或两个中。 (不,我不能将过期字段放在出版物上,因为它仅针对该特定角色过期 - 它可能是当前的不同角色)
此数据库是:
role
{
role_id int (PK)
role_name varchar(50)
}
role_pub
{
role_id int (PK)
pub_id int (PK)
is_expired bit
}
pub
{
pub_id int (PK)
pub_name varchar(50)
}
流畅的映射:
public class RoleMapping : ClassMap<Role>
{
public RoleMapping()
{
Table("role");
Id(x => x.Id, "role_id").GeneratedBy.Identity();
Map(x => x.Name, "role_name").Not.Nullable();
HasManyToMany<Publication>(x => x.CurrentPublications)
.Table("role_pub")
.ParentKeyColumn("role_id")
.ChildKeyColumn("pub_id")
.Where("is_expired = 0")
.LazyLoad();
HasManyToMany<Publication>(x => x.ExpiredPublications)
.Table("role_pub")
.ParentKeyColumn("role_id")
.ChildKeyColumn("pub_id")
.Where("is_expired = 1")
.LazyLoad();
}
}
public class PublicationMapping : ClassMap<Publication>
{
public PublicationMapping()
{
Table("pub");
Id(x => x.Id, "pub_id").GeneratedBy.Identity();
Map(x => x.Name, "pub_name").Not.Nullable();
}
}
当我执行选择时在角色上,当前和过期出版物填充了正确的出版物,但是当我将新出版物添加到当前或过期列表时,它总是将 is_expired 字段保存为 0,这是默认值数据库中的“is_expired”。
有谁知道映射此关系并正确填充“is_expired”字段的正确方法是什么?
感谢您的帮助
萨恩
I'm having an issue with persisting a field which is in my were clause on a ManyToMany releationship, but doesn't explicityly exist on the class mapping. Hard to explain but its the "is_expred" field in the below classes and mappings.
I have the following two classes:
public class Publication
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
}
public class Role
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual IEnumerable<Publication> CurrentPublications {get; set;}
public virtual IEnumerable<Publication> ExpiredPublications {get; set;}
}
Where a publication can exist in either or both the CurrentPublications and ExpiredPublications. (and no, I can't put the expired field on the Publication, as its only expired for this particular role - it might be current on a different role)
The DB for this is:
role
{
role_id int (PK)
role_name varchar(50)
}
role_pub
{
role_id int (PK)
pub_id int (PK)
is_expired bit
}
pub
{
pub_id int (PK)
pub_name varchar(50)
}
And the fluent mapping:
public class RoleMapping : ClassMap<Role>
{
public RoleMapping()
{
Table("role");
Id(x => x.Id, "role_id").GeneratedBy.Identity();
Map(x => x.Name, "role_name").Not.Nullable();
HasManyToMany<Publication>(x => x.CurrentPublications)
.Table("role_pub")
.ParentKeyColumn("role_id")
.ChildKeyColumn("pub_id")
.Where("is_expired = 0")
.LazyLoad();
HasManyToMany<Publication>(x => x.ExpiredPublications)
.Table("role_pub")
.ParentKeyColumn("role_id")
.ChildKeyColumn("pub_id")
.Where("is_expired = 1")
.LazyLoad();
}
}
public class PublicationMapping : ClassMap<Publication>
{
public PublicationMapping()
{
Table("pub");
Id(x => x.Id, "pub_id").GeneratedBy.Identity();
Map(x => x.Name, "pub_name").Not.Nullable();
}
}
When I do the select on a role, the Current and Expired Publications are populated with the correct Publications, but when I add new publciations to either Current or Expired lists, it always saves the is_expired field as 0, which is the default value of "is_expired" in the DB.
Does anyone have any idea on wha tthe correct way of mapping this releationship and populating the "is_expired" field correctly?
Thanks for your help
Saan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
where 仅在 SELECT 上进行过滤,但绝不会填充 INSERT 上的列。您可以引入一个类
RolePublication
,其中映射了is_expired
the where only filters on SELECT but never fills up the columns on INSERT. you could introduce a class
RolePublication
which has theis_expired
mapped