Tôi đang chuyển một dự án cũ sang ASP.NET 5 và Entity Framework 7. Tôi đã sử dụng phương pháp tiếp cận cơ sở dữ liệu đầu tiên (khung DNX) để tạo mô hình.Entity Framework 7 log kiểm toán
Các dự án cũ được dựa trên Entity Framework 4 và theo dõi kiểm toán được thực hiện bằng cách ghi đè các phương pháp SaveChanges
của DbContext
:
public override int SaveChanges(System.Data.Objects.SaveOptions options)
{
int? UserId = null;
if (System.Web.HttpContext.Current != null)
UserId = (from user in Users.Where(u => u.UserName == System.Web.HttpContext.Current.User.Identity.Name) select user.Id).SingleOrDefault();
foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
{
Type EntityType = entry.Entity.GetType();
PropertyInfo pCreated = EntityType.GetProperty("Created");
PropertyInfo pCreatedById = EntityType.GetProperty("CreatedById");
PropertyInfo pModified = EntityType.GetProperty("Modified");
PropertyInfo pModifiedById = EntityType.GetProperty("ModifiedById");
if (entry.State == EntityState.Added)
{
if (pCreated != null)
pCreated.SetValue(entry.Entity, DateTime.Now, new object[0]);
if (pCreatedById != null && UserId != null)
pCreatedById.SetValue(entry.Entity, UserId, new object[0]);
}
if (pModified != null)
pModified.SetValue(entry.Entity, DateTime.Now, new object[0]);
if (pModifiedById != null && UserId != null)
pModifiedById.SetValue(entry.Entity, UserId, new object[0]);
}
}
return base.SaveChanges(options);
}
Câu hỏi của tôi là, làm thế nào tôi có thể thực hiện điều này trong Entity Framework 7? Tôi có phải tiếp cận mã đầu tiên không?
EF7 không được hoàn thành và không sẵn sàng cho sản xuất. Tôi sẽ đi cho EF6 và DbContext API đầu tiên. Vẫn còn nhiều mục trên backlog EF7 phải được thực hiện trước khi nó đủ trưởng thành. –
Sự hiểu biết của tôi là bạn có thể thực hiện nó theo cùng một cách ... hoặc về cơ bản giống nhau. – Seabizkit
Cá nhân bạn nên chuyển id người dùng vào một phương thức, thay vì tham chiếu System.Web trong một lớp dữ liệu liên quan. – Seabizkit