如何首先使用 trackerenableddbcontext 在 asp.net mvc5 和代码中的实体框架中实现审计跟踪
How to implement audit trail in asp.net mvc5 and entity-framework in code first, using trackerenableddbcontext
我正在使用 Entity framework 和代码优先迁移开发 MVC5 应用程序,我想实施审计跟踪。我找到了一个名为 trackerenableddbcontext 的 NuGet 包,它看起来很容易使用。然而,我确实安装了这个包,并做了以下代码,但仍然没有任何东西被添加到审计中 table.
我已经实现了从Tracker Context继承的,如代码所示
DBContext class
public class SIContext : TrackerContext //DbContext
{
public SIContext()
: base("name=SIConnectionString")
{
}
public virtual DbSet<Role> Roles { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Menu> Menus { get; set; }
public virtual DbSet<MenuPermission> MenuPermissions { get; set; }
public virtual DbSet<ProductCategory> ProductCategorys { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Tax> Taxs { get; set; }
public virtual DbSet<ProductStock> ProductStocks { get; set; }
public virtual DbSet<PaymentMode> PaymentModes { get; set; }
public virtual DbSet<Sale> Sales { get; set; }
public virtual DbSet<InvoiceItems> InvoiceItemss { get; set; }
public virtual DbSet<Setting> Settings { get; set; }
public virtual DbSet<InvoiceFormat> InvoiceFormats { get; set; }
public virtual DbSet<InventoryType> InventoryTypes { get; set; }
public virtual DbSet<Purchase> Purchases { get; set; }
public virtual DbSet<Transaction> Transactions { get; set; }
public virtual DbSet<Invoice> Invoices { get; set; }
public virtual DbSet<LedgerAccount> LedgerAccounts { get; set; }
public virtual DbSet<Warehouse> Warehouses { get; set; }
public virtual DbSet<DuePayment> DuePayments { get; set; }
public virtual DbSet<Expense> Expenses { get; set; }
public virtual DbSet<DeliveryNote> DeliveryNotes { get; set; }
public virtual DbSet<GRV> GRVs { get; set; }
public virtual DbSet<Artisans> Artisans { get; set; }
public virtual DbSet<JobCard> JobCards { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<InternalRequisitionForm> InternalRequisitionForms { get; set; }
public virtual DbSet<JobRequisition> JobRequisitions { get; set; }
public virtual DbSet<Stores> Stores { get; set; }
public virtual DbSet<Machine> Machines { get; set; }
public virtual DbSet<ListItems> listitem { get; set; }
public virtual DbSet<JobCardServices> JobCardServices { get; set; }
public virtual DbSet<JobCardMaterials> JobCardMaterials { get; set; }
public virtual DbSet<JobRequisitionMaterials> JobRequisitionMaterials { get; set; }
public virtual DbSet<JobRequisitionServices> JobRequisitionServices { get; set; }
public virtual DbSet<GRVMaterials> GRVMaterials { get; set; }
public virtual DbSet<Quotation> Quotations { get; set; }
public virtual DbSet<QuotationItems> QuotationItems { get; set; }
public virtual DbSet<DNoteMaterial> DNoteMaterials { get; set; }
public virtual DbSet<InvoiceMaterials> InvoiceMaterial { get; set; }
//
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ShopMate.Maping.RoleMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.UserMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.MenuMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.MenuPermissionMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.ProductCategoryMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.ProductMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.TaxMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.ProductStockMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.PaymentModeMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.SaleMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InvoiceItemsMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.SettingMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InvoiceFormatMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InventoryTypeMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.PurchaseMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.TransactionMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InvoiceMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.LedgerAccountMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.WarehouseMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.DuePaymentMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.ExpenseMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InternalRequisitionFormMap());
base.OnModelCreating(modelBuilder);
}
}
Table我要跟踪
[TrackChanges]
public class Product
{
[DisplayName("S.No")]
public int Id { get; set; }
[Required]
[StringLength(100)]
[DisplayName("Name")]
public string Name { get; set; }
[Required]
[DisplayName("Product Category")]
public int? ProductCategoryId { get; set; }
public virtual ProductCategory ProductCategory_ProductCategoryId { get; set; }
[StringLength(100)]
[DisplayName("Bar Code")]
public string BarCode { get; set; }
[Required]
[DisplayName("Purchase Price")]
public Decimal PurchasePrice { get; set; }
[Required]
[DisplayName("Sale Price")]
public Decimal SalePrice { get; set; }
[DisplayName("Product Image")]
public string ProductImage { get; set; }
[DisplayName("Added By")]
public Nullable<int> AddedBy { get; set; }
[DisplayName("Date Added")]
public Nullable<DateTime> DateAdded { get; set; }
[DisplayName("Modified By")]
public Nullable<int> ModifiedBy { get; set; }
[DisplayName("Date Modied")]
public Nullable<DateTime> DateModied { get; set; }
[Required]
[DisplayName("Is Active")]
public bool IsActive { get; set; }
[Required]
[DisplayName("Stock Alert")]
public int StockAlert { get; set; }
[DisplayName("Tax")]
public Nullable<int> TaxId { get; set; }
[Required]
[DisplayName("Warehouse")]
public int WarehouseId { get; set; }
[DisplayName("Discount")]
public Nullable<Decimal> Discount { get; set; }
[DisplayName("Remaining Quantity")]
public Nullable<Decimal> RemainingQuantity { get; set; }
[DisplayName("Remaining Amount")]
public Nullable<Decimal> RemainingAmount { get; set; }
[StringLength(100)]
[DisplayName("HSN")]
public string HSN { get; set; }
public virtual ICollection<ProductStock> ProductStock_ProductIds { get; set; }
public virtual ICollection<Sale> Sale_ProductIds { get; set; }
public virtual ICollection<InvoiceItems> InvoiceItems_ProductIds { get; set; }
public virtual ICollection<Purchase> Purchase_ProductIds { get; set; }
}}
控制器功能
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Edit(Product ObjProduct, HttpPostedFileBase ProductImage, string HideImage1)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int userId = Convert.ToInt32(Env.GetUserInfo("userid"));
try
{
if (ModelState.IsValid)
{
if (ProductImage != null)
{
var fileName = MicrosoftHelper.MSHelper.StarkFileUploaderCSharp(ProductImage, Server.MapPath("~/Uploads"));
ModelState.Clear();
ObjProduct.ProductImage = fileName;
}
else
{
ObjProduct.ProductImage = HideImage1;
}
db.Entry(ObjProduct).State = EntityState.Modified;
db.SaveChanges(userId);
sb.Append("Sumitted");
return Content(sb.ToString());
}
else
{
foreach (var key in this.ViewData.ModelState.Keys)
{
foreach (var err in this.ViewData.ModelState[key].Errors)
{
sb.Append(err.ErrorMessage + "<br/>");
}
}
}
}
catch (Exception ex)
{
sb.Append("Error :" + ex.Message);
}
return Content(sb.ToString());
}
我希望它将更改保存到数据库、audidlog 和 auditlogdetails tables。
当您在 DBContext 中使用附加实体时,您需要在应用程序启动时添加 GlobalTrackingConfig.DisconnectedContext = true;
。
我正在使用 Entity framework 和代码优先迁移开发 MVC5 应用程序,我想实施审计跟踪。我找到了一个名为 trackerenableddbcontext 的 NuGet 包,它看起来很容易使用。然而,我确实安装了这个包,并做了以下代码,但仍然没有任何东西被添加到审计中 table.
我已经实现了从Tracker Context继承的,如代码所示
DBContext class
public class SIContext : TrackerContext //DbContext
{
public SIContext()
: base("name=SIConnectionString")
{
}
public virtual DbSet<Role> Roles { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Menu> Menus { get; set; }
public virtual DbSet<MenuPermission> MenuPermissions { get; set; }
public virtual DbSet<ProductCategory> ProductCategorys { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Tax> Taxs { get; set; }
public virtual DbSet<ProductStock> ProductStocks { get; set; }
public virtual DbSet<PaymentMode> PaymentModes { get; set; }
public virtual DbSet<Sale> Sales { get; set; }
public virtual DbSet<InvoiceItems> InvoiceItemss { get; set; }
public virtual DbSet<Setting> Settings { get; set; }
public virtual DbSet<InvoiceFormat> InvoiceFormats { get; set; }
public virtual DbSet<InventoryType> InventoryTypes { get; set; }
public virtual DbSet<Purchase> Purchases { get; set; }
public virtual DbSet<Transaction> Transactions { get; set; }
public virtual DbSet<Invoice> Invoices { get; set; }
public virtual DbSet<LedgerAccount> LedgerAccounts { get; set; }
public virtual DbSet<Warehouse> Warehouses { get; set; }
public virtual DbSet<DuePayment> DuePayments { get; set; }
public virtual DbSet<Expense> Expenses { get; set; }
public virtual DbSet<DeliveryNote> DeliveryNotes { get; set; }
public virtual DbSet<GRV> GRVs { get; set; }
public virtual DbSet<Artisans> Artisans { get; set; }
public virtual DbSet<JobCard> JobCards { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<InternalRequisitionForm> InternalRequisitionForms { get; set; }
public virtual DbSet<JobRequisition> JobRequisitions { get; set; }
public virtual DbSet<Stores> Stores { get; set; }
public virtual DbSet<Machine> Machines { get; set; }
public virtual DbSet<ListItems> listitem { get; set; }
public virtual DbSet<JobCardServices> JobCardServices { get; set; }
public virtual DbSet<JobCardMaterials> JobCardMaterials { get; set; }
public virtual DbSet<JobRequisitionMaterials> JobRequisitionMaterials { get; set; }
public virtual DbSet<JobRequisitionServices> JobRequisitionServices { get; set; }
public virtual DbSet<GRVMaterials> GRVMaterials { get; set; }
public virtual DbSet<Quotation> Quotations { get; set; }
public virtual DbSet<QuotationItems> QuotationItems { get; set; }
public virtual DbSet<DNoteMaterial> DNoteMaterials { get; set; }
public virtual DbSet<InvoiceMaterials> InvoiceMaterial { get; set; }
//
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ShopMate.Maping.RoleMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.UserMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.MenuMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.MenuPermissionMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.ProductCategoryMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.ProductMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.TaxMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.ProductStockMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.PaymentModeMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.SaleMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InvoiceItemsMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.SettingMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InvoiceFormatMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InventoryTypeMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.PurchaseMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.TransactionMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InvoiceMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.LedgerAccountMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.WarehouseMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.DuePaymentMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.ExpenseMap());
modelBuilder.Configurations.Add(new ShopMate.Maping.InternalRequisitionFormMap());
base.OnModelCreating(modelBuilder);
}
}
Table我要跟踪
[TrackChanges]
public class Product
{
[DisplayName("S.No")]
public int Id { get; set; }
[Required]
[StringLength(100)]
[DisplayName("Name")]
public string Name { get; set; }
[Required]
[DisplayName("Product Category")]
public int? ProductCategoryId { get; set; }
public virtual ProductCategory ProductCategory_ProductCategoryId { get; set; }
[StringLength(100)]
[DisplayName("Bar Code")]
public string BarCode { get; set; }
[Required]
[DisplayName("Purchase Price")]
public Decimal PurchasePrice { get; set; }
[Required]
[DisplayName("Sale Price")]
public Decimal SalePrice { get; set; }
[DisplayName("Product Image")]
public string ProductImage { get; set; }
[DisplayName("Added By")]
public Nullable<int> AddedBy { get; set; }
[DisplayName("Date Added")]
public Nullable<DateTime> DateAdded { get; set; }
[DisplayName("Modified By")]
public Nullable<int> ModifiedBy { get; set; }
[DisplayName("Date Modied")]
public Nullable<DateTime> DateModied { get; set; }
[Required]
[DisplayName("Is Active")]
public bool IsActive { get; set; }
[Required]
[DisplayName("Stock Alert")]
public int StockAlert { get; set; }
[DisplayName("Tax")]
public Nullable<int> TaxId { get; set; }
[Required]
[DisplayName("Warehouse")]
public int WarehouseId { get; set; }
[DisplayName("Discount")]
public Nullable<Decimal> Discount { get; set; }
[DisplayName("Remaining Quantity")]
public Nullable<Decimal> RemainingQuantity { get; set; }
[DisplayName("Remaining Amount")]
public Nullable<Decimal> RemainingAmount { get; set; }
[StringLength(100)]
[DisplayName("HSN")]
public string HSN { get; set; }
public virtual ICollection<ProductStock> ProductStock_ProductIds { get; set; }
public virtual ICollection<Sale> Sale_ProductIds { get; set; }
public virtual ICollection<InvoiceItems> InvoiceItems_ProductIds { get; set; }
public virtual ICollection<Purchase> Purchase_ProductIds { get; set; }
}}
控制器功能
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Edit(Product ObjProduct, HttpPostedFileBase ProductImage, string HideImage1)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int userId = Convert.ToInt32(Env.GetUserInfo("userid"));
try
{
if (ModelState.IsValid)
{
if (ProductImage != null)
{
var fileName = MicrosoftHelper.MSHelper.StarkFileUploaderCSharp(ProductImage, Server.MapPath("~/Uploads"));
ModelState.Clear();
ObjProduct.ProductImage = fileName;
}
else
{
ObjProduct.ProductImage = HideImage1;
}
db.Entry(ObjProduct).State = EntityState.Modified;
db.SaveChanges(userId);
sb.Append("Sumitted");
return Content(sb.ToString());
}
else
{
foreach (var key in this.ViewData.ModelState.Keys)
{
foreach (var err in this.ViewData.ModelState[key].Errors)
{
sb.Append(err.ErrorMessage + "<br/>");
}
}
}
}
catch (Exception ex)
{
sb.Append("Error :" + ex.Message);
}
return Content(sb.ToString());
}
我希望它将更改保存到数据库、audidlog 和 auditlogdetails tables。
当您在 DBContext 中使用附加实体时,您需要在应用程序启动时添加 GlobalTrackingConfig.DisconnectedContext = true;
。