-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4aa65b5
commit f8e26e0
Showing
13 changed files
with
194 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
|
||
namespace Audentity; | ||
|
||
public record EntityTrace( | ||
Type Type, | ||
EntityState State, | ||
IReadOnlyCollection<PropertyTrace> Properties, | ||
IReadOnlyCollection<ReferenceTrace> References) | ||
{ | ||
public static EntityTrace FromEntry(EntityEntry entry) | ||
{ | ||
IReadOnlyCollection<PropertyTrace> properties = | ||
entry | ||
.Properties | ||
.Select(PropertyTrace.FromEntry) | ||
.ToArray(); | ||
|
||
IReadOnlyCollection<ReferenceTrace> references = | ||
entry | ||
.Navigations | ||
.SelectMany(ReferenceTrace.FromEntry) | ||
.ToArray(); | ||
|
||
return new EntityTrace(entry.Entity.GetType(), entry.State, properties, references); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Audentity | ||
|
||
**Auditing library for Entity Framework** | ||
|
||
## Usage | ||
|
||
Collect traces by catching the state of the `Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker` | ||
before saving changes. That can be done simply by overriding the `SaveChanges()` & `SaveChangesAsync(CancellationToken)` | ||
methods in your `Microsoft.EntityFrameworkCore.DbContext` implementation. | ||
|
||
```csharp | ||
public class MyDbContext : DbContext | ||
{ | ||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new()) | ||
{ | ||
ImmutableList<EntityTrace> traces = ChangeTracker.Entries() | ||
.Select(EntityTrace.FromEntry) | ||
.ToList(); | ||
|
||
int result = await base.SaveChangesAsync(cancellationToken); | ||
// Process traces... | ||
return result; | ||
} | ||
} | ||
``` | ||
|
||
### Shadow Entries | ||
|
||
If you have many-to-many relationships in your database model, Entity Framework will generate a shadow entity that | ||
represents a reference between two entities - unless you have defined such an entity yourself. | ||
|
||
```csharp | ||
public class Project | ||
{ | ||
public Guid Id { get; set; } | ||
public IEnumerable<User> Users { get; set; } | ||
} | ||
|
||
public class User | ||
{ | ||
public Guid Id { get; set; } | ||
public IEnumerable<Project> Projects { get; set; } | ||
} | ||
|
||
// Shadow entity generated by Entity Framework: | ||
public class ProjectUser | ||
{ | ||
public Guid ProjectId { get; set; } | ||
public Guid UserId { get; set; } | ||
} | ||
``` | ||
|
||
Those entities, even if they are not defined in the code itself, will still end up in our trace collection. | ||
To exclude them from traces, you can filter all entries by their CLR type before collecting traces. | ||
|
||
```csharp | ||
ChangeTracker.Entries() | ||
.Where(e => e.Metadata.ClrType != typeof(Dictionary<string, object>)) | ||
.Select(EntityTrace.FromEntry); | ||
``` |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.