1

We have the following Domain objects :-

public class UserDevice : BaseObject
{
// different properties to hold data
}

public class DeviceRecipient:BaseObject
{
 public virtual UserDevice LastAttemptedDevice{get;set;}
}

Hence the sql schema created based on this using fluent nhibernate automapper is like DeviceRecipient's table is having primary key of UserDevice as a foreign key i.e UserDevice_Id.

Now, When we try to delete UserDevice object it gives a sql exception for foreign key constraint. What we want to do is :-

  1. Delete the UserDevice object , hence the UserDevice row without deleting the DeviceRecipient as it will be used somewhere else in domain model. We just want to set null to UserDevice_Id column of DeviceRecipient when we delete UserDevice.
  2. We want to do it using fluent nhibernate conventions as we use Automapping.

Any help will be appreciable.. Thanks in advance.!

hazzik
  • 13,019
  • 9
  • 47
  • 86
Niraj
  • 376
  • 4
  • 14

1 Answers1

1

As I can see you have uni-direction many-to-one relation. So firstly you have to write following override:

public class DeviceRecipientOverride : IAutoMappingOverride<DeviceRecipient>
{
    public void Override(AutoMapping<DeviceRecipient> mapping)
    {
        mapping.References(x => x.LastAttemptedDevice)
            .NotFound.Ignore(); // this doing what you want.
    }
}

Secondly you could convert it to automapping convention, if you have more places with this behavior.

public class ManyToOneNullableConvention : IReferenceConvention
{
    public void Apply(IManyToOneInstance instance)
    {
        var inspector = (IManyToOneInspector) instance;
        // also there you could check the name of the reference like following:  
        // inspector.Name == LastAttemptedDevice
        if (inspector.Nullable) 
        {
            instance.NotFound.Ignore();
        }
    }
}

EDIT:

From the NHibernate reference

not-found (optional - defaults to exception): Specifies how foreign keys that reference missing rows will be handled: ignore will treat a missing row as a null association.

So when you set not-found="ignore" SchemaExport/SchemaUpdate will just not create the FK for you. So if you have the FK then you need to delete it or set OnDelete behavior of the FK to Set Null. Assuming that you are using Microsoft Sql Server:

ALTER TABLE [DeviceRecipient] 
    ADD CONSTRAINT [FK_DeviceRecipient_LastAttemptedDevice] 
    FOREIGN KEY ([LastAttemptedDevice_ID]) 
    REFERENCES [UserDevice]
    ON DELETE SET NULL
hazzik
  • 13,019
  • 9
  • 47
  • 86
  • i did try them both..but problem still persists.. i checked in database the foreign key is allowed for NULL. Any other check i need to make? – Niraj Jul 24 '12 at 09:03
  • Ok, I will take a look, but possible that when `not-found="ignore"` is used then SchemaExport/SchemaUpdate just does not create FK. I will take a more deeper look.. – hazzik Jul 24 '12 at 10:10
  • Yes...Setting OnDelete behavior of the FK to Set NULL worked..! – Niraj Jul 25 '12 at 12:09