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