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 826435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:24:54+00:00 2026-05-15T03:24:54+00:00

I’m importing data that may or may not exist already in my database. I’d

  • 0

I’m importing data that may or may not exist already in my database. I’d like NHibernate to associate any entities with the existing db one if it exists (probably just setting the primary key/id), or create a new one if it doesn’t. I’m using S#arp architecture for my framework (MVC 2, NHibernate, Fluent).

I’ve added the [HasUniqueDomainSignature] attribute to the class, and a [DomainSignature] attribute to the properties I want to use for comparison. The only way I can think to do it (which is not an acceptable solution and may not even work) is the following (psuedo C#):

foreach (Book importedBook in importedBooks){
    foreach (Author author in importedBook.Authors){
        if (!author.IsValid()){  // NHibernate Validator will check DomainSignatures
            author = _authorRepository.GetByExample(author);  // This would be to get the db object with the same signature, 
                                 //but I don't think I could even update this as I iterate through it.
        }
}

}

As you can see, this is both messy, and non-sensical. Add to that the fact that I’ve got a half dozen associations on the Book (subject, format, etc), and it doesn’t make any sense. There’s got to be an easy way to do this that I’m missing. I’m not a novice with NHibernate, but I’m definitely not an expert.

  • 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-05-15T03:24:55+00:00Added an answer on May 15, 2026 at 3:24 am

    Just realize I never gave an answer or approved another’s answer. I ended up just writing a new SaveOrUpdate which takes a parameter to check for existing before persisting. I also added an attribute to my domain models to overwrite when saving/updating (although in retrospect it’s only on updating that it’d be overwriting).

    Here’s the code if it can help anyone else in this dilemma:

           public TEntity SaveOrUpdate<TEntity>(TEntity entity, bool checkForExistingEntity)
        {
            IRepository<TEntity> repository = new Repository<TEntity>();
            if (checkForExistingEntity) {
    
                if (entity is Entity) {
                    IEnumerable<PropertyInfo> props = (entity as Entity).GetSignatureProperties();
                    Dictionary<string, object> parameters =
                        props.ToDictionary(propertyInfo => propertyInfo.Name, propertyInfo => propertyInfo.GetValue(entity, null));
                    TEntity duplicateEntity = repository.FindOne(parameters);
                    if (duplicateEntity != null) {
                        // Update any properties with the OverwriteOnSaveUpdate attribute
                        foreach (var property in RepositoryHelper.GetUpdatableProperties(typeof(TEntity)))
                        {
                            object initialValue = property.GetValue(entity, null);
                            property.SetValue(duplicateEntity, initialValue, null);
                        }
                        // Fill in any blank properties on db version
                        foreach (var property in typeof(TEntity).GetProperties())
                        {
                            if (property.GetValue(duplicateEntity, null) == null) {
                                object initialValue = property.GetValue(entity, null);
                                property.SetValue(duplicateEntity, initialValue, null);
                            }
                        }
                        return duplicateEntity;
                    }
                }
            }
            return SaveOrUpdate(entity);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.