Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:25:16+00:00 2026-06-08T05:25:16+00:00

We have the following Domain objects :- public class UserDevice : BaseObject { //

  • 0

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.!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:25:19+00:00Added an answer on June 8, 2026 at 5:25 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following domain object: public class Data { public virtual int ID
I have the following domain objects: public class UserSpecialization : PersonSpecialization, IUserSpecialization { public
If I have the following domain object: public class Customer { public virtual Guid
I have the following domain: public class FileInformation { public String FileName; public String
I have the following Domain Object: public class DomainClass { public int Id {
I have the following domain object: public class DomainObject<T,TRepo> where T : DomainObject<T> where
I have the following domain object: public class Bank : IEntity { } And
So let's say we have a domain object such as the following public class
I have the following 2 objects: User and DomainUser User.java: package com.domain; import java.io.Serializable;
I have the following Grails domain class: class Product { String name Float basePrice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.