NHibernate One to One Foreign Key ON DELETE CASCADE
        Posted  
        
            by 
                xll
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by xll
        
        
        
        Published on 2012-12-13T16:52:40Z
        Indexed on 
            2012/12/13
            17:03 UTC
        
        
        Read the original article
        Hit count: 319
        
nhibernate
|fluent-nhibernate
I need to implement One-to-one association between Project and ProjecSettings using fluent NHibernate:
public class ProjectMap : ClassMap<Project>
{
   public ProjectMap()
   {
      Id(x => x.Id)
        .UniqueKey(MapUtils.Col<Project>(x => x.Id))
        .GeneratedBy.HiLo("NHHiLoIdentity", "NextHiValue", "1000",
           string.Format("[EntityName] = '[{0}]'", MapUtils.Table<Project>()))
        .Not.Nullable();
      HasOne(x => x.ProjectSettings)
        .PropertyRef(x => x.Project);
    }
}
public class ProjectSettingsMap : ClassMap<ProjectSettings>
{
    public ProjectSettingsMap()
    {
      Id(x => x.Id)
        .UniqueKey(MapUtils.Col<ProjectSettings>(x => x.Id))
        .GeneratedBy.HiLo("NHHiLoIdentity", "NextHiValue", "1000",
          string.Format("[EntityName] = '[{0}]'", MapUtils.Table<ProjectSettings>()));
      References(x => x.Project)
        .Column(MapUtils.Ref<ProjectSettings, Project>(p => p.Project, p => p.Id))
        .Unique()
        .Not.Nullable();
    }
}
This results in the following sql for Project Settings:
CREATE TABLE ProjectSettings (
  Id                       bigint PRIMARY KEY NOT NULL,
  Project_Project_Id       bigint NOT NULL UNIQUE,
  /* Foreign keys */
  FOREIGN KEY (Project_Project_Id)
    REFERENCES Project()
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
);
What I am trying to achieve is to have ON DELETE CASCADE for the FOREIGN KEY (Project_Project_Id), so that when the project is deleted through sql query, it's settings are deleted too. How can I achieve this ?
EDIT: I know about Cascade.Delete() option, but it's not what I need. Is there any way to intercept the FK statement generation?
© Stack Overflow or respective owner