Coder Social home page Coder Social logo

hassanhabib / efxceptions Goto Github PK

View Code? Open in Web Editor NEW
31.0 7.0 24.0 107 KB

.NET Standard library to convert DbUpdateException into meaningful exceptions

Home Page: https://www.efxceptions.com

C# 100.00%
entityframeworkcore entityframework sql exceptions

efxceptions's Introduction

.NET The Standard - COMPLIANT

EFxceptions

preview version

We have designed and developed this library as a wrapper around the existing EntityFramework DbContext implementation to provide the following values:

  1. Meaningful Exceptions for SQL error codes.
  2. Simplified integrations
  3. Test-friendly implementation.

EFxeptions.Identity

preview version

A dedicated EFxeptions port that provides an EFxceptionContext that inherits from Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext to inherit from, to support Microsoft ASP.Core Identity using EF Core. Available in the EFxceptions.Identity package.

Installation

You can get EFxceptions Nuget package by typing:

Install-Package EFxceptions

Integration

Replace your existing DbContext class with EFxceptionsContext (or your IdentityDbContext with EFxeption.EFxceptionIdentityContext) as follows:

Before:

    public partial class StorageBroker : DbContext, IStorageBroker
    {
        public StorageBroker(DbContextOptions<StorageBroker> options)
            : base(options) => this.Database.Migrate();
    }

After:

    public partial class StorageBroker : EFxceptionsContext, IStorageBroker
    {
        public StorageBroker(DbContextOptions<StorageBroker> options)
            : base(options) => this.Database.Migrate();
    }

Supported SQL Error Codes

SQL server supports over 41,000 error codes, here's the codes that this library supports so far:

Code Meanings Exception
207 Invalid column name '%.*ls'. InvalidColumnNameException
208 Invalid object name '%.*ls'. InvalidObjectNameException
547 The %ls statement conflicted with the %ls constraint "%.*ls". The conflict occurred in database "%.*ls", table "%.*ls"%ls%.*ls%ls. ForeignKeyConstraintConflictException
2627 Violation of %ls constraint '%.*ls'. Cannot insert duplicate key in object '%.*ls'. DuplicateKeyException

This library is forever growing as we add more exceptions and codes into it, we appreciate any contributions as there are so many codes we need to cover, so please stay tuned.


If you have any suggestions, comments or questions, please feel free to contact me on:
Twitter: @hassanrezkhabib
LinkedIn: hassanrezkhabib
E-Mail: [email protected]

efxceptions's People

Contributors

aliceluo2048 avatar cjdutoit avatar hassanhabib avatar imjane5 avatar mabroukmahdhi avatar sasestil avatar shrihumrudha avatar weitzhandler avatar ysnarafat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

efxceptions's Issues

Exception handling does not follow C# guideline

In the past we all followed Microsoft's design guideline regarding exception handling. I'm wondering if it still applies to .NET Core / .NET Standard.

If so, you might be hiding information or make it harder to find the root cause.

Let me show you an example:

public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
    try
    {
        return await base.SaveChangesAsync(cancellationToken);
    }
    catch (DbUpdateException dbUpdateException)
    {
        this.eFxceptionService.ThrowMeaningfulException(dbUpdateException);
        throw dbUpdateException; // <-- That's the problem
        throw; // <-- would be better
    }
}

MAJOR FIX: Serialize Problem Details

Guys i'm working on a source generator using the code from this repo.

The non SQLServer mappings have a bug. The test is failing to detect it as it is testing for a number it itself is throwing but its the wrong number.

On a duplicate for an indexed property reading the wrong property etc..

image
image
image
image

Exception: Unable to cast object of type 'MySql.Data.MySqlClient.MySqlException' to type 'Microsoft.Data.SqlClient.SqlException'

Hello,
I am using this great library but for a MySql connection, and I can only get this exception:
Unable to cast object of type MySql.Data.MySqlClient.MySqlException to type Microsoft.Data.SqlClient.SqlException

By checken the code, i found this:

       public void ThrowMeaningfulException(DbUpdateException dbUpdateException)
        {
            ValidateInnerException(dbUpdateException);
            SqlException sqlException = GetSqlException(dbUpdateException.InnerException);
            int sqlErrorCode = this.sqlErrorBroker.GetSqlErrorCode(sqlException);
            ConvertAndThrowMeaningfulException(sqlErrorCode, sqlException.Message);

            throw dbUpdateException;
        }

I guess the library should be inhanced to support also MySqlException at least.

I forked the repo and fixed it, should I create a pull request ?

Thanks

Not working in postgre

EFxceptions does not support postgre I get an error like below.

Unable to cast object of type 'Npgsql.PostgresException' to type 'Microsoft.Data.SqlClient.SqlException'.

Consider using other Database Providers for Exceptions

I was thinking somewhere along the lines of using generics or some switch statement that takes in the value and based on that it uses the specific database provider(mssql,mysql,postgres,sqlite etc) with the specific sqlexception handling. Any help would be appreciate it

Using wrong SqlClient

EF Core 3 uses Microsoft.Dats.SqlClient. using System.Data.SqlClient with this package is wrong and will cause confusion for consumers and increase deployment size for no good reason.

DOCUMENTATION: Add Images Folder Future Nuget Pkgs Fixes

Adds an assets folder named images with a set of supporting pre done images, icons, master psd and git header logo for future nuget package uploads and or automation builds.

Readme updated with nuget badges appropriately for downloads and community.

Once committed/merged the current readme.md header html block for rendering the EFxceptions.png file can be replaced with the markdown syntax.
Sample, may need be redefined post: ![}(https://raw.githubusercontent.com/hassanhabib/EFxceptions/master/EFxceptions.Shared/Resources/git_logo.png)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.