I’ve inherited a database and I’m trying to map it to Fluent NHibernate.
I have the following schema:
CREATE TABLE [Signatures](
[Id] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL,
-- Other Fields....
)
CREATE TABLE [SignoffSteps](
[Id] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL,
[SignatureId] [int] NULL REFERENCES [Signatures]([Id]),
-- Other Fields....
)
And I’m attempting to map it to the following
(Extra properties and mappings omitted):
public class SignoffStep
{
public virtual int Id { get; set; }
public virtual Signature Signature { get; set;}
public class Map : ClassMap<SignoffStep>
{
public Map()
{
Table("SignoffSteps");
Id(x => x.Id);
References(x => x.Signature, "SignatureId")
.Nullable()
.ForeignKey("FK_SingoffSteps_Signatures")
.Cascade.All()
.Not.LazyLoad();
}
}
}
public class Signature
{
public virtual int Id { get; set; }
public class Map : ClassMap<Signature>
{
public Map()
{
Table("Signatures");
Id(x => x.Id);
}
}
}
This works great, except for one annoyance: deleting Signatures. I can make it work if I manually delete the signature:
session.Delete(signoffStep.Signature);
signoffStep.Signature = null;
But what I’d like to do is just set the Signature property on the signoff step to null, and have NHibernate automatically delete the child. Is there a way to setup my mapping to do this?
EDIT: Setting Cascade to “all-delete-orphan” not doable:

I ended up having a “ToDelete” property on the Signature, and manually deleting them while I’m saving. I don’t like this b/c it break the abstraction but it works for now.
I think to really fix it I wanted to, I would have had to redo the tables and associations a bit. Unfortunately this was out of the question in this case.