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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:59:24+00:00 2026-06-15T02:59:24+00:00

I’m having a hard time figuring out where to catch Entity Framework exceptions. I

  • 0

I’m having a hard time figuring out where to catch Entity Framework exceptions. I have SaveChanges and concurrency exceptions covered, however, I’m having a hard time finding a general rule to catch other exceptions (like timeouts because the server went down, or something).

I’m doing a Windows client application so exceptions cannot just spit out a “error” web page, they must give out options to continue the application (since you might have other windows opened in it, this is no simple app).

This was quite easy to figure out when using ADO.net directly: you’d trap exceptions on connection opening and query executing, however I’m not quite sure exactly when are connections opened and queries executed using EF4 (database-first in this case).

There are some simple cases (for example, when I actually run the enumerator on the IQueryable, by calling ToList or ToArray), but there are others which are not that easily found: for example, if I’m assigning an IQueryable to a WinForm control’s DataSource, depending on the control behaviour (which I’m not always controlling), it may or may not actually execute the query (some controls execute it upon calling DataBind later).

I know I could do this by trial and error (or just doing huge try/catch blocks), but I’m trying to figure out a more general way to do this.

I’ve tried searching for some onexception-like event on the ObjectContext, but haven’t found anything remotely near, and everything I find on the matter is about exceptions on SaveChanges and concurrency exceptions, which as I said, I have covered.

Any hints on where to go?

  • 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-15T02:59:27+00:00Added an answer on June 15, 2026 at 2:59 am

    are you familiar with the concept of unit of work? this typically defines the boundaries of when a context begins and ends. exception handling can then be placed in/around the unit of work to handle specific exceptions.

    here is just one idea.

    public void Execute(Action<DbContext> action)
    {
        try
        {
           using(var context = new MyContext())
           {
               action(context);
               context.SaveChanges();
           }
        }
        catch(ConcurrencyException exception)
        {
        }
        catch(ValidationException exception)
        {
        }
        catch(SqlException exception)
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.