Created
February 14, 2019 14:32
-
-
Save danielplawgo/fa316d8a8b790f5ee57f06c1d78cbffe to your computer and use it in GitHub Desktop.
Konfiguracja 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
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.Configuration.Exclude<Product>(); | |
return audit; | |
} |
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
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.Configuration.Exclude(o => o is Product && ((Product)o).Name == "Product Name"); | |
return audit; | |
} |
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
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.Configuration.Exclude(o => true); | |
//audit.Configuration.Include<Product>(); | |
audit.Configuration.Include(o => o is Product && ((Product)o).Name == "Product Name"); | |
return audit; | |
} |
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
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.Configuration.ExcludeProperty<BaseModel>(t => t.IsActive); | |
return audit; | |
} |
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
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.Configuration.ExcludeProperty<Product>(); | |
audit.Configuration.IncludeProperty<Product>(t => t.Name); | |
return audit; | |
} |
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
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.Configuration.Format<Product>(x => x.Price, x => ((decimal)x).ToString("0.00 zl")); | |
return audit; | |
} |
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
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.Configuration.IgnorePropertyUnchanged = false; | |
return audit; | |
} |
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
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
audit.Configuration.SoftDeleted<BaseModel>(x => x.IsActive == false); | |
return audit; | |
} |
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 = ConfigureAudit(); | |
audit.PreSaveChanges(this); | |
var rowAffecteds = base.SaveChanges(); | |
audit.PostSaveChanges(); | |
if (audit.Configuration.AutoSavePreAction != null) | |
{ | |
audit.Configuration.AutoSavePreAction(this, audit); | |
base.SaveChanges(); | |
} | |
return rowAffecteds; | |
} | |
private Audit ConfigureAudit() | |
{ | |
var audit = new Audit(); | |
audit.CreatedBy = UserName; | |
return audit; | |
} | |
} |
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 Product : BaseModel | |
{ | |
public string Name { get; set; } | |
public int CategoryId { get; set; } | |
public virtual Category Category { get; set; } | |
public string Description { get; set; } | |
public decimal Price { 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(); | |
SoftDeleteTest(); | |
DeleteTest(); | |
ShowHistory(id); | |
} | |
static int AddTest() | |
{ | |
using (DataContext db = new DataContext()) | |
{ | |
var category = db.Categories.FirstOrDefault(); | |
var product = new Product() | |
{ | |
Name = "Product Name", | |
Category = category, | |
Description = "Product Description", | |
Price = 10.23M | |
}; | |
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 SoftDeleteTest() | |
{ | |
using (DataContext db = new DataContext()) | |
{ | |
var product = db.Products | |
.OrderByDescending(c => c.Id) | |
.FirstOrDefault(); | |
product.IsActive = false; | |
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(); | |
} | |
} | |
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