1

I would like to implement nlog to each action to add an element. So when I do myContext.Society.Add(), I would like to log something. I create a class DbSetExtension and modify the context StockContext to use DbSetExtension<T> instead DbSet.

public class DbSetExtension<T> : DbSet<T> where T : class
{
    public override T Add(T entity)
    {
        LoggerInit.Current().Trace("Add Done");
        return base.Add(entity);
    }
}

When i launch the programm, I notice when I access to myContext.Society.Add. Society is null. So I think I miss something with my class DbSetExtension but I don't find.

public class StockContext : DbContext
{

    public StockContext()
        : base("StockContext")
    {
    }

    public DbSet<HistoricalDatas> HistoricalDatas { get; set; }
    public DbSet<Society> Society { get; set; }  

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();           
    }
}

Do you have any idea, Regards, Alex

[UPDATE] Code allows to add. If I replace DbSetExtension by DbSet, the same code works. So my assumption is I miss something when I inherit from DbSet.

    public bool SetSymbols()
    {            
        CsvTools csvThreat = new CsvTools();
        List<Eoddata> currentEnum =
            csvThreat.ExtractData<Eoddata>(ConfigurationManager.GetString("FilePathQuotes", ""));
        currentEnum.ForEach(
            c =>
                {
                    //LoggerInit.Current().Trace("Add Done");
                    Sc.Society.Add(
                        new Society()
                        {
                            RealName = c.Description,
                            Symbol = String.Format("{0}.PA", c.Symbol),
                            IsFind = !String.IsNullOrEmpty(c.Description)
                        });
                });            
        if (Sc.SaveChanges() > 0)
            return true;
        return false;
    }
3
  • can you share this part of your code: myContext.Society.Add. Commented Apr 6, 2015 at 14:39
  • I add the code relating to the action add? Commented Apr 6, 2015 at 20:24
  • I add the code to add action. Commented Apr 6, 2015 at 20:24

1 Answer 1

2

In my opinion you took totally wrong direction. DbContext is made to work with DbSet and not DbSetExtension class. It is able to instantiate objects of type DbSet and not your own type. This is basically why you get this exception. Reparing it would require probably hacking EF internals and I fear that this problem will be just a beginning for you. Instead I would recommend you to use general way of logging with EF with use of interceptor classes. Here this is explained in details at the end of article Logging and Intercepting Database Operations. Generally this approach would be much more advantageous for you. Why? Because DbContext is just man-in-the-middle in communication with db. In logs you generally cares about what happens to db and its data. Calling Add method on DbSet may not have any effect at all if SaveChanges won't be called lated on. On contrary query interceptors lets you log strictly only interaction with db. Basing on query sent to db you may distinguish what is going on.

But if you instist on your approach I would recommend you using extension methods instead of deriving from DbSet:

public static class DbSetExtensions
{
    public static T LoggingAdd<T>(this DbSet<T> dbSet, T entity)
    {
        LoggerInit.Current().Trace("Add Done");
        return dbSet.Add(entity);
    }
}

and call it like this:

context.Stock.LoggingAdd(entity);
Sign up to request clarification or add additional context in comments.

1 Comment

Hi mr100, thank you for your answer. Your proposal fit my needs so I implements "logging and intercepting database operaitons". Futhermore, your sample about the extension is right too. Really thanks, I think effectively I was on the wrong way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.