Entity Framework

Learn about Sentry's .NET integration with Entity Framework.

Sentry provides an integration with EntityFramework through the of the Sentry.EntityFramework NuGet package.

Looking for EntityFramework Core? If you're targeting .NET Core 3.1 or newer, that's already built in to the main Sentry .NET SDK. It's also enabled by default for our ASP.NET or ASP.NET Core integrations, even for older targets. For other scenarios, you may need to add the Sentry.DiagnosticSource package and call AddDiagnosticSourceIntegration, as described here. (The rest of this page describes our EF6 integration, not EFCore.)

  • Queries as breadcrumbs
  • Validation errors

All queries executed are added as breadcrumbs and are sent with any event which happens on the same scope. Besides that, validation errors are also included as Extra.

In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing.

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

Copied
Install-Package Sentry.EntityFramework -Version 4.12.1

This package extends Sentry main SDK. That means that besides the EF features, through this package you'll also get access to all API and features available in the main Sentry SDK.

Add the Entity Framework 6 support to your project in one step:

  • When initializing the SDK, call the extension method AddEntityFramework() on SentryOptions. This will register all error processors to extract extra data, such as validation errors, from the exceptions thrown by Entity Framework.

For example, configuring an ASP.NET app with global.asax:

global.asax
Copied
using System;
using System.Configuration;
using Sentry;
using Sentry.AspNet;

public class MvcApplication : System.Web.HttpApplication
{
    private IDisposable _sentrySdk;

    protected void Application_Start()
    {
        _sentrySdk = SentrySdk.Init(options =>
        {
            // We store the DSN inside Web.config
            options.Dsn = ConfigurationManager.AppSettings["SentryDsn"];
            // Set traces_sample_rate to 1.0 to capture 100% of transactions for performance monitoring.
            // We recommend adjusting this value in production.
            options.TracesSampleRate = 1.0;
            // Add the EntityFramework integration
            options.AddEntityFramework();
        });
    }

    // Global error catcher
    protected void Application_Error() => Server.CaptureLastError();

    protected void Application_BeginRequest()
    {
        Context.StartSentryTransaction();
    }

    protected void Application_EndRequest()
    {
        Context.FinishSentryTransaction();
    }

    public override void Dispose()
    {
        _sentrySdk.Dispose();
        base.Dispose();
    }
}

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.

Copied
using Sentry;

try
{
    throw null;
}
catch (Exception ex)
{
    SentrySdk.CaptureException(ex);
}

Check out a complete working sample to see it in action.

Sample breadcrumbs in Sentry

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").