I'm trying to save a new parent item & new child item to a legacy database
I get my data just fine, when I save it saves both the parent and the child. however the Child's SillyNameParentId is always 0
The table structure I can't change so I have to figure out how to make this work.
Here is the SQL generated
NHibernate: INSERT INTO SillyNameParent(Description, Active) VALUES (@p0, @p1); select SCOPE_IDENTITY();@p0 = 'Test' [Type: String (1073741823)], @p1 = True [Type: Boolean (0)] NHibernate: INSERT INTO SillyChild (SillyNameCategoryId, sillyNameParentid) VALUES (@p0, @p1); select SCOPE_IDENTITY();@p0 = 0 [Type: Int32 (0)], @p1 = 1 [Type: Int32 (0)]
Ultimately I want @p1 to be set as SillyNameParentId/Parentid
Tables:
SillyNameParent
Column PK-IsIdentity: ParentId
Column varchar(255): Description
Column bit: Active
SillyChild //I'm a lookup table
Column int IsIdentity: Id
Column int PK: SillyNameParentId
Column int PK: SillyNameCategoryID
MODELS:
public class SillyNameParent: Entity
{
public SillyNameParent()
{
Children= new List<SillyChild>();
}
public virtual string AreaOfConcernDesc { get; set; }
public virtual bool Active { get; set; }
public virtual IList<SillyChild> Children{ get; set; }
}
public class SillyChild: Entity
{
public virtual int SillyNameParentId { get; set; }
public virtual int SillyNameCategoryId{ get; set; }
public virtual SillyNameParent Parent { get; set; }
}
MAPS:
public class SillyNameParentMap : IAutoMappingOverride<SillyNameParent>
{
public void Override(AutoMapping<SillyNameParent> mapping)
{
mapping.Table("SillyNameParent");
mapping.Id(x => x.Id).Column("ParentId").GeneratedBy.Identity();
mapping.Map(x => x.Description).Not.Nullable();
mapping.Map(x => x.Active).Nullable();
mapping.HasMany(x => x.children)
.Cascade.All()
.KeyColumn("SillyNameParentId")
.Not
.LazyLoad();
}
}
public class SillyChildMap: IAutoMappingOverride<SillyChild>
{
public void Override(AutoMapping<SillyChild> mapping)
{
mapping.Table("SillyChild");
mapping.Id(x => x.Id).Column("Id").GeneratedBy.Identity();
mapping.Map(x => x.SillyNameParentId).Not.Nullable();
mapping.Map(x => x.SillyNameCategoryId).Not.Nullable();
mapping.HasOne(x => x.Parent).ForeignKey("SillyNameParentId");
}
}