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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:07:30+00:00 2026-06-08T09:07:30+00:00

Since EF doesn’t support unique key contraints, it seems that we need to catch

  • 0

Since EF doesn’t support unique key contraints, it seems that we need to catch exception during the Save method, and display error message to user.

The problems with this approach are:

  • how do we know which record threw an exception
  • how do we know what kind of problem threw an exception (ex I could have two unique constraints on same record, so I need to tell the user which one is broken)

DBMS is SqlServer 2008.

How to resolve these problems?

  • 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-08T09:07:32+00:00Added an answer on June 8, 2026 at 9:07 am

    If you allow that a user can enter values that must be unique in the database you should validate this input before you save the changes:

    if (context.Customers.Any(c => c.SomeUniqueProperty == userInput))
        // return to user with a message to change the input value
    else
        context.SaveChanges();
    

    This isn’t only the case for values with unique constraints in the database but also for entering foreign key values that must refer to existing target records or primary key values if the primary key isn’t autogenerated in the database. EF doesn’t help you in the latter situation either because a context doesn’t know the content of the whole database table but only about the entities that are currently attached to the context. It is true that EF will forbid to attach two objects with the same primary key but allows two objects with the same unique key constraint. But this doesn’t guard you completely against primary key constraint violations when you save changes to the database.

    In the unlikely case that between the Any check and SaveChanges another user has entered a record with the same value, I would consider the occuring exception as not possible to handle and just tell the user “An expected error occurred, please try again…”. If the user tries again the Any check happens again and he will get the more useful message to change the input value from the code above.

    The exception returned for such unique key constraint or primary key constraint violations is a general DbUpdateException and one of the inner exceptions will be a SqlException that contains as one of its properties a SQL Server error code. Any other details can be found only in the exception message “Violation of UNIQUE KEY constraint IX_SomeUniqueProperty_Index…” or similar. If you expect that the user can understand and react accordingly to this information you could display it. Otherwise you could log this message for an administrator or developer to check for possible bugs or other problems.

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

Sidebar

Related Questions

Since Exchange doesn't provide support for all local calendar types in OWA, I need
Since Flash doesn't support multithread how do you that in actionscript 3 ?
Since VBScript doesn't support lookbehinds, I'm looking for an alternative solution. I have the
Since iphone doesn't support flash at all. Is it Darwin streaming server ?
On my current project I'm using SQL CE. Since it doesn't have support for
Since silverlight doesn't have complete TimeZoneInfo I will need to implement some of the
PHP is somewhat crippled since it doesn't have return types (yet). I need my
I overheard an argument about System.out.print() today. One person claimed that since print() doesn't
I need to use a byte[] as a key in a Dictionary . Since
Since ListCollectionView doesn't support indexer (ex View[0]), the only way I found to iterate

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.