Created
February 11, 2019 13:58
-
-
Save danielplawgo/5f2ea5c4d5f3f41e0566b4b5d1abb667 to your computer and use it in GitHub Desktop.
Audit z Entity Framework Plus
This file contains hidden or 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
public class DataContext : DbContext | |
{ | |
public DataContext() | |
: base("Name=DefaultConnection") | |
{ | |
} | |
public DbSet<Category> Categories { get; set; } | |
public DbSet<Product> Products { get; set; } | |
} |
This file contains hidden or 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
public class DataContext : DbContext | |
{ | |
static DataContext() | |
{ | |
AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) => | |
(context as DataContext).AuditEntries.AddRange(audit.Entries); | |
} | |
public DataContext() | |
: base("Name=DefaultConnection") | |
{ | |
} | |
public DbSet<Category> Categories { get; set; } | |
public DbSet<Product> Products { get; set; } | |
public DbSet<AuditEntry> AuditEntries { get; set; } | |
public DbSet<AuditEntryProperty> AuditEntryProperties { get; set; } | |
} |
This file contains hidden or 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
public class DataContext : DbContext | |
{ | |
static DataContext() | |
{ | |
AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) => | |
(context as DataContext).AuditEntries.AddRange(audit.Entries); | |
} | |
public DataContext() | |
: base("Name=DefaultConnection") | |
{ | |
} | |
public DbSet<Category> Categories { get; set; } | |
public DbSet<Product> Products { get; set; } | |
public DbSet<AuditEntry> AuditEntries { get; set; } | |
public DbSet<AuditEntryProperty> AuditEntryProperties { get; set; } | |
public string UserName { get; set; } = "System"; | |
public override int SaveChanges() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.PreSaveChanges(this); | |
var rowAffecteds = base.SaveChanges(); | |
audit.PostSaveChanges(); | |
if (audit.Configuration.AutoSavePreAction != null) | |
{ | |
audit.Configuration.AutoSavePreAction(this, audit); | |
base.SaveChanges(); | |
} | |
return rowAffecteds; | |
} | |
} |
This file contains hidden or 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
public class BaseModel | |
{ | |
public BaseModel() | |
{ | |
IsActive = true; | |
} | |
public int Id { get; set; } | |
public bool IsActive { get; set; } | |
} | |
public class Category : BaseModel | |
{ | |
public string Name { get; set; } | |
public virtual ICollection<Product> Products { get; set; } | |
} | |
public class Product : BaseModel | |
{ | |
public string Name { get; set; } | |
public int CategoryId { get; set; } | |
public virtual Category Category { get; set; } | |
} |
This file contains hidden or 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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int id = AddTest(); | |
EditTest(); | |
DeleteTest(); | |
} | |
static int AddTest() | |
{ | |
using (DataContext db = new DataContext()) | |
{ | |
var category = db.Categories.FirstOrDefault(); | |
var product = new Product() | |
{ | |
Name = "Product Name", | |
Category = category | |
}; | |
db.Products.Add(product); | |
db.SaveChanges(); | |
return product.Id; | |
} | |
} | |
static void EditTest() | |
{ | |
using (DataContext db = new DataContext()) | |
{ | |
var category = db.Categories | |
.OrderByDescending(c => c.Id) | |
.FirstOrDefault(); | |
var product = db.Products | |
.OrderByDescending(c => c.Id) | |
.FirstOrDefault(); | |
product.Category = category; | |
product.Name = "New Product Name"; | |
db.SaveChanges(); | |
} | |
} | |
static void DeleteTest() | |
{ | |
using (DataContext db = new DataContext()) | |
{ | |
var product = db.Products | |
.OrderByDescending(c => c.Id) | |
.FirstOrDefault(); | |
db.Products.Remove(product); | |
db.SaveChanges(); | |
} | |
} | |
} |
This file contains hidden or 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
static int AddTest() | |
{ | |
using (DataContext db = new DataContext()) | |
{ | |
var category = db.Categories.FirstOrDefault(); | |
var product = new Product() | |
{ | |
Name = "Product Name", | |
Category = category | |
}; | |
db.Products.Add(product); | |
var audit = new Audit(); | |
audit.CreatedBy = "UserName"; | |
db.SaveChanges(audit); | |
return product.Id; | |
} | |
} |
This file contains hidden or 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
static void ShowHistory(int id) | |
{ | |
using (DataContext db = new DataContext()) | |
{ | |
var histories = db.AuditEntries.Where<Product>(id); | |
foreach (var history in histories) | |
{ | |
Console.WriteLine($"State: {history.State}"); | |
foreach (var property in history.Properties) | |
{ | |
Console.WriteLine($"Propepty: {property.PropertyName}, OldValue: {property.OldValue}, NewValue: {property.NewValue}"); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment