bilal-fazlani / tracker-enabled-dbcontext

Tracker-enabled DbContext offers you to implement full auditing in your database
https://tracker-enabled-dbcontext.bilal-fazlani.com/
Other
217 stars 110 forks source link

Can we make the models partials #78

Open BradleyBarnett opened 8 years ago

BradleyBarnett commented 8 years ago

I wanted to extend the auditlog entity but can't as they are not partials. If I instead inherit AuditLog, entity framework won't play nice making us add discriminator columns and alike causing a number of issues.

Can we change all your models to partials.

bilal-fazlani commented 8 years ago

Hi Brad, I am happy to convert the required class to partials. I would be even happier if you fork , do it, then create a pull request. Let me know.

On Mon, Dec 14, 2015 at 10:17 AM, Brad Barnett notifications@github.com wrote:

I wanted to extend the auditlog entity but can't as they are not partials If I instead inherit AuditLog, entity framework won't play nice making us add discriminator columns and alike causing a number of issues

Can we change all your models to partials

— Reply to this email directly or view it on GitHub https://github.com/bilal-fazlani/tracker-enabled-dbcontext/issues/78.

Thanks & regards

Bilal Fazlani

BradleyBarnett commented 8 years ago

Sure I'll fork it and submit a pull request today.

Cheers, Brad On 14 Dec 2015 5:50 pm, "Bilal" notifications@github.com wrote:

Hi Brad, I am happy to convert the required class to partials. I would be even happier if you fork , do it, then create a pull request. Let me know.

On Mon, Dec 14, 2015 at 10:17 AM, Brad Barnett notifications@github.com wrote:

I wanted to extend the auditlog entity but can't as they are not partials If I instead inherit AuditLog, entity framework won't play nice making us add discriminator columns and alike causing a number of issues

Can we change all your models to partials

— Reply to this email directly or view it on GitHub https://github.com/bilal-fazlani/tracker-enabled-dbcontext/issues/78.

Thanks & regards

Bilal Fazlani

— Reply to this email directly or view it on GitHub https://github.com/bilal-fazlani/tracker-enabled-dbcontext/issues/78#issuecomment-164360272 .

talmroth commented 8 years ago

I would also like this. I will fork and submit pull.

BradleyBarnett commented 8 years ago

I tried this but didn't submit, it didn't get me what I wanted as partials can only work within their own assembly, so trying to extend with a partial class in your own project doesn't work.

talmroth commented 8 years ago

Yeah, wasn't thinking clearly there.

I have an idea that I will work out tomorrow that I think can work.

On Wed, Mar 2, 2016 at 6:50 PM, Brad Barnett notifications@github.com wrote:

I tried this but didn't submit, it didn't get me what I wanted as partials can only work within their own assembly, so trying to extend with a partial class in your own project doesn't work.

— Reply to this email directly or view it on GitHub https://github.com/bilal-fazlani/tracker-enabled-dbcontext/issues/78#issuecomment-191552504 .

talmroth commented 8 years ago

This is much more involved that I had thought. I was working though making everything generic so you could choose to use subclasses of AuditLog and/or AuditLogDetails. Unfortunately it means making just about everything generic which will take quite a bit more time.

Should I continue down this path or would there be a better way to handle this. I desperately need to extend the logging tables for things specific to my application. I could do this in the log event to a different table but that doesn't feel right.

BradleyBarnett commented 8 years ago

What are you trying to add the the logging tables? I have this working quite well. As soon as you have sub classes you get the issues with entity framework wanting to add discriminator columns to differentiate each sub class.

Worth noting that hooking in directly to change tracking in entity framework isn't that hard either. So you could just do it custom.

Add something like the below code to your repository save method.

var entities = dbContext.ChangeTracker.Entries() .Where(x => x.Entity is IEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));

talmroth commented 8 years ago

It only does the discriminator thing if your DbSet is of the base type. If the dbSet is of the derived type it will be a single table. So the 2 logging DbSets would be of the generic types where were restricted to the base logging entities.

We need to log some additional information that wouldn't be something most people would want to do. I could accomplish this by just adding my own table and writing my additional data in the event that is raised. But it would be nice to have it in one table and not have to worry about referential integrity and cascading deletes and such.

I was in design phase of my own version of this when I found this on nuget. It was very similar to what I was doing but mine was not nearly as full featured so I figured why reinvent the wheel.

bilal-fazlani commented 8 years ago

How about using the event to store everything you want at separate table/location. And using event argument u can skip saving it in the default audit table. If having everything in one table is you want.

Do u have any idea to make it more generic or extendable ?

bilal-fazlani commented 8 years ago

I am thinking to integrate this with serilog to that people can log it anywhere they want

talmroth commented 8 years ago

I was going down this path basically:

public class TrackerContext<TA, TAD> : DbContext, ITrackerContext
    where TA : AuditLog
    where TAD : AuditLogDetail

With this I could extend the log models and create a TrackerContext with my derived types. This would work but I would have to basically refactor almost everything to be generic.

I'm not sure if that feels right. I think you and @TooMuchTaurine are right that having the event do its own logging is probably the way to go. I was so focused on making it work that I wasn't thinking clearly about how doing it myself would work.

Not sure if I like the idea of using serilog unless its optional. I don't want more dependencies if I am going to use EF for the logs. But I am not very familiar with serilog so my position is just a general feeling.

bilal-fazlani commented 8 years ago

I have started working on serilog integration . There are a lot of benefits. You get to chose

I have it working on a separate branch. https://github.com/bilal-fazlani/tracker-enabled-dbcontext/pull/97

BradleyBarnett commented 8 years ago

Sounds good, i will have to upgrade after you land it..

On Fri, Mar 11, 2016 at 6:35 PM, Bilal notifications@github.com wrote:

I have started working on serilog integration . There are a lot of benefits. You get to chose

I have it working on a separate branch. #97 https://github.com/bilal-fazlani/tracker-enabled-dbcontext/pull/97

— Reply to this email directly or view it on GitHub https://github.com/bilal-fazlani/tracker-enabled-dbcontext/issues/78#issuecomment-195234026 .

bilal-fazlani commented 8 years ago

https://github.com/bilal-fazlani/tracker-enabled-dbcontext/wiki/7.-Metadata