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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:14:25+00:00 2026-05-27T06:14:25+00:00

I have an application that creates entities in offline mode then saves them to

  • 0

I have an application that creates entities in offline mode then saves them to the database when it is available. Many entities may be created while the application is offline.

Here is a basic version of the method:

public void SaveEntityToDatabase(Entity entity)            
{
    Context.Entities.AddObject(entity);
    Context.SaveChanges();
}

Since these entities are created offline there is no way to tell if there are existing entities with similar data. For instance, I have a field “SomeText” that must be unique across records. This field is type ‘text’ in SQL Server so rather than try to directly compare the value I hash it and compare the hash value. If there are no matching records I go ahead and add the new entity otherwise I break out of the method early.

Here is a basic version of the updated method:

public void SaveEntityToDatabase(Entity entity)            
{
    var hashedValue = entity.SomeText.ToSHA1();

    if (Context.Entities.Where(e => e.SomeTextHash == hashedValue).Count() > 0)
        return;

    entity.SomeTextHash = hashedValue;

    Context.Entities.AddObject(entity);
    Context.SaveChanges();
}

Unfortunately doing the existence check causes my app to throw the exception:

Cannot insert the value NULL into column 'SomeText'.

Commenting out the check fixes the problem but doesn’t allow for me to check existing records.

//if (Context.Entities.Where(e => e.SomeTextHash == hashedValue).Count() > 0)
    //return;

If I insert a breakpoint at SaveChanges and inspect ‘entity’ if is fully populated. It seems that the existence check is invalidating ‘entity’ in the object graph but I am not familiar enough with the internal workings of Entity Framework to be sure.

What is causing that exception and how can I avoid it?

  • 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-27T06:14:26+00:00Added an answer on May 27, 2026 at 6:14 am

    Ultimately it looks like doing the check was resetting my context. I needed to track my changes separately then add the entity after the hashvalue check was completed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an application that creates bar graph based on inputs read from
I have created an application that allows users to capture information for entities based
I have an ASP.NET MVC3 application that uses entities generated from a database. Each
I have an existing application that has the following entities in the database Customer
I have an application that creates invalid Directories... e.g. c:\Program Files\somedirectory. - the period
I have application that brings response via Ajax and creates 5-20 new jQuery click
I have a .Net application that dynamically creates a small HTML page and pops
Today we have a windows application that, using an OCX, creates a web page
I have created an application that connect on a web-server and reads an xml
I have created an application that uses settings.settings to store some user specific settings

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.