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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:38:59+00:00 2026-05-26T19:38:59+00:00

I am using Entity Framework and when a DbUpdateException is thrown from dbContext.SaveChanges() how

  • 0

I am using Entity Framework and when a DbUpdateException is thrown from dbContext.SaveChanges() how do I create a custom exception and throw that instead?

Would I create a condition on each SQL constraint that can be thrown:

if (e.InnerException.InnerException.Message.Contains("UNIQUE KEY"))
{
     throw new CustomException("message");
}
  • 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-26T19:38:59+00:00Added an answer on May 26, 2026 at 7:38 pm

    EDIT: That approach makes good sense to me. If you know your application/DB is going to have a specific error, and it will help you or your users to have a specific custom exception type that quickly identifies what would otherwise be a somewhat complex or specific scenario, then absolutely yes. It’s good practice to use both the exception type and the exception message to make the error as clear as possible. My code below is an even simpler example than what you seem to drilling down into. Rather than letting other code end up with a null reference exception or some other consequence, I beat everything to the punch with throw new DatabaseDataNotFoundException("Cannot find ServerAppConfiguration value for {0}", key);.

    Just make your own exception class that inherits from Exception, here’s a custom exception I use for exactly that purpose:

    public class DatabaseDataNotFoundException : Exception
    {
        public DatabaseDataNotFoundException() : base() { }
    
        public DatabaseDataNotFoundException(string message) : base(message) { }
    
        public DatabaseDataNotFoundException(string message, params object[] args)
            : base(String.Format(CultureInfo.CurrentCulture, message, args)) { }
    
        public DatabaseDataNotFoundException(string message, Exception inner)
            : base(message, inner) { }
    
        public DatabaseDataNotFoundException(string message, Exception inner, params object[] args)
            : base(String.Format(CultureInfo.CurrentCulture, message, args), inner) { }
    
        protected DatabaseDataNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
    }
    

    Then your code becomes:

    if (e.InnerException.InnerException.Message.Contains("UNIQUE KEY"))
    {
        throw new DatabaseDataNotFoundException("message");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Entity Framework 4 and code first how would I create a model that
When using Entity Framework 4, how do you create a single entity from a
I'm using Entity Framework O/R mapper from Microsoft and using entity classes (generated classes
I'm using Entity Framework in my project, and I have the problem that, once
I have a problem with the following Linq query using Entity Framework: from o
I am using Entity Framework from .NET 3.5 I have two tables with 0-1
I'm using entity framework and i have a problem that this dropdown ignores my
I'm using Entity Framework 4 for a simple app and would like to bake
I'm using entity framework 4. I have a stored procedure that just updates one
I'm using Entity Framework 4 and I have created a UnitOfWork class that creates

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.