tracker-enabled-dbcontext icon indicating copy to clipboard operation
tracker-enabled-dbcontext copied to clipboard

Can we make the models partials

Open BradleyBarnett opened this issue 9 years ago • 14 comments

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.

BradleyBarnett avatar Dec 14 '15 04:12 BradleyBarnett

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 [email protected] 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

bilal-fazlani avatar Dec 14 '15 06:12 bilal-fazlani

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

Cheers, Brad On 14 Dec 2015 5:50 pm, "Bilal" [email protected] 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 [email protected] 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 .

BradleyBarnett avatar Dec 14 '15 19:12 BradleyBarnett

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

talmroth avatar Mar 02 '16 22:03 talmroth

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.

BradleyBarnett avatar Mar 03 '16 02:03 BradleyBarnett

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 [email protected] 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 avatar Mar 03 '16 03:03 talmroth

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.

talmroth avatar Mar 04 '16 01:03 talmroth

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));

BradleyBarnett avatar Mar 04 '16 04:03 BradleyBarnett

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.

talmroth avatar Mar 04 '16 05:03 talmroth

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 avatar Mar 04 '16 14:03 bilal-fazlani

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

bilal-fazlani avatar Mar 04 '16 15:03 bilal-fazlani

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.

talmroth avatar Mar 04 '16 23:03 talmroth

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

  • What you want to log
    • https://github.com/serilog/serilog/wiki/Configuration-Basics#filters
    • https://github.com/serilog/serilog/wiki/Configuration-Basics#enrichers
  • What is the format of logs
    • https://github.com/serilog/serilog/wiki/Structured-Data
  • Where you want to log
    • https://github.com/serilog/serilog/wiki/Provided-Sinks#from-the-serilog-project

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

bilal-fazlani avatar Mar 11 '16 07:03 bilal-fazlani

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

On Fri, Mar 11, 2016 at 6:35 PM, Bilal [email protected] wrote:

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

  • What you want to log - https://github.com/serilog/serilog/wiki/Structured-Data
  • What is the format of logs - https://github.com/serilog/serilog/wiki/Structured-Data
  • Where you want to log - https://github.com/serilog/serilog/wiki/Provided-Sinks#from-the-serilog-project

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 .

BradleyBarnett avatar Mar 11 '16 08:03 BradleyBarnett

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

bilal-fazlani avatar Jun 21 '16 19:06 bilal-fazlani