監査ログを追加するには、EFのSaveChangesをそのまま上書きしているようです。以下の監査プロパティ(created、createdby、updated、updatedby)を設定するには、ApplyAuditLoggingメソッドを参照してください。Entity Framework 5 SaveChangesを使用して監査ログを追加する
public override int SaveChanges()
{
var autoDetectChanges = Configuration.AutoDetectChangesEnabled;
try
{
Configuration.AutoDetectChangesEnabled = false;
ChangeTracker.DetectChanges();
var errors = GetValidationErrors().ToList();
if(errors.Any())
{
throw new DbEntityValidationException("Validation errors were found during save: " + errors);
}
foreach (var entry in ChangeTracker.Entries().Where(e => e.State == EntityState.Added || e.State == EntityState.Modified))
{
ApplyAuditLogging(entry);
}
ChangeTracker.DetectChanges();
Configuration.ValidateOnSaveEnabled = false;
return base.SaveChanges();
}
finally
{
Configuration.AutoDetectChangesEnabled = autoDetectChanges;
}
}
private static void ApplyAuditLogging(DbEntityEntry entityEntry)
{
var logger = entityEntry.Entity as IAuditLogger;
if (logger == null) return;
var currentValue = entityEntry.Cast<IAuditLogger>().Property(p => p.Audit).CurrentValue;
if (currentValue == null) currentValue = new Audit();
currentValue.Updated = DateTime.Now;
currentValue.UpdatedBy = "???????????????????????";
if(entityEntry.State == EntityState.Added)
{
currentValue.Created = DateTime.Now;
currentValue.CreatedBy = "????????????????????????";
}
}
問題は、どのようにオブジェクトのUpdatedByとCREATEDBYプロパティを設定するには、Windowsのユーザーのログオン/ユーザ名を取得するということですか?私はこれを使用することはできませんでした!
また、別のケースでは、私の連絡先に新しいCallHistoryレコードを自動的に追加したいと思っていました。連絡先が変更されるたびに、新しいレコードを子テーブルのCallHistoryに追加する必要があります。リポジトリのInsertOrUpdateで行ったのですが、データベースから現在のユーザーを設定する必要があるため、リポジトリのInsertOrUpdateでリポジトリのInsertOrUpdateで行いましたが、汚いと感じました。ここでも問題は、CallHistoryレコード(SalesRep = User)を作成するためにデータベースからユーザーをフェッチする必要があることです。
ContactRepository.SetCurrentUser(User).InsertOrUpdate(contact)
:
私のリポジトリ内のコードは1、今2つのことを行い、それが2、作成または更新されたオブジェクトの監査エントリを作成し、それはまた、連絡先が更新されるたびにCallHistoryエントリを作成しました以下のためのリポジトリのコンテキストでユーザーを持つために
:
var prop = typeof(T).GetProperty("Id", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (prop.GetValue(entity, null).ToString() == "0")
{
// New entity
_context.Set<T>().Add(entity);
var auditLogger = entity as IAuditLogger;
if (auditLogger != null)
auditLogger.Audit = new Audit(true, _principal.Identity.Name);
}
else
{
// Existing entity
_context.Entry(entity).State = EntityState.Modified;
var auditLogger = entity as IAuditLogger;
if (auditLogger != null && auditLogger.Audit != null)
{
(entity as IAuditLogger).Audit.Updated = DateTime.Now;
(entity as IAuditLogger).Audit.UpdatedBy = _principal.Identity.Name;
}
var contact = entity as Contact;
if (_currentUser != null)
contact.CallHistories.Add(new CallHistory
{
CallTime = DateTime.Now,
Contact = contact,
Created = DateTime.Now,
CreatedBy = _currentUser.Logon,
SalesRep = _currentUser
});
}
}
は何とかDbContextでのSaveChangesオーバーライドにWindowsユーザーを注入する方法があるともからユーザーを取得する方法がありますデータベースは、WindowsログオンIDに基づいて私はサルを設定することができます私のCallHistoryでesRep(上記のコードを参照)?ここで
はMVCアプリのコントローラ上の私の処置:
[HttpPost]
public ActionResult Create([Bind(Prefix = "Contact")]Contact contact, FormCollection collection)
{
SetupVOs(collection, contact, true);
SetupBuyingProcesses(collection, contact, true);
var result = ContactRepository.Validate(contact);
Validate(result);
if (ModelState.IsValid)
{
ContactRepository.SetCurrentUser(User).InsertOrUpdate(contact);
ContactRepository.Save();
return RedirectToAction("Edit", "Contact", new {id = contact.Id});
}
var viewData = LoadContactControllerCreateViewModel(contact);
SetupPrefixDropdown(viewData, contact);
return View(viewData);
}
したがって、SamAccountNameは変更可能であり、したがって良好な識別子ではありません。代わりにすべてのオブジェクトが取得するアクティブディレクトリguidを使用します – meffect