Entity Framework Connection error event

c# entity-framework entity-framework-6

Question

I'm attempting to capture every SQLException that EF may give in one location, including connection errors, timeouts, etc. There is no need for a unit of work pattern since I'm utilising DI.var using = context for instance. caller IDs:.Single or .ToList are used across business logic.

Is there a class hook or event that I could replace or inject to do this?

1
1
1/31/2018 8:52:15 AM

Popular Answer

You still have some work to do, but this might be helpful:

encompass youDbContext Under a false framework, conceal yourDbSets You create a new context that will house your business logic. Access to the is not provided by this one.DbSets The user is presented with custom-dbSets. The primary task will be to construct the fake-context, which must include all of the methods you use on the actual dbSets.

/// <summary>
/// This sould no be used anywhere except in MyDbContext2
/// </summary>
public class MyDbContext : DbContext
{
    public DbSet<SomeTableClass> SomeTable { get; set; }
}

/// <summary>
/// This will be your context in the business-logic
/// </summary>
public class MyDbContext2
{
    private MyDbContext realDb;

    public MyDbContext2(string conStr)
    {
        realDb = new MyDbContext();
    }

    public MyDbSet<SomeTableClass> SomeFakeTable { get; set; }
}

/// <summary>
/// Fake-set with logging
/// </summary>
/// <typeparam name="T"></typeparam>
public class MyDbSet<T> where T : class
{
    private DbSet<T> dbSet;

    public MyDbSet(DbSet<T> realDbSet)
    {
        this.dbSet = realDbSet;
    }

    public List<T> ToList()
    {
        try
        {
            return dbSet.ToList();
        }
        catch (Exception e)
        {
            // Do some logging..
            throw;
        }
    }

    public T SingleOrDefault()
    {
        try
        {
            return dbSet.SingleOrDefault();
        }
        catch (Exception e)
        {
            // Do some logging..
            throw;
        }
    }
}

Renaming you would be a nice idea.DbContext then provide the impostor with the old name. Visual Studio will then display the methods you need to use in this manner.

0
1/31/2018 9:26:54 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow