我们公司是使用 EF 的新手,我将不胜感激有关我们问题的任何指导/建议。在这个项目中,我们使用EF 6,Visual Studio 2015,数据库优先方法和RESTful服务(这是一个使用JSON而不是通用API的RESTful API,我们只需要审计日志记录方法是通用的(。我们需要实施审核,并正在使用 ChangeTracker 功能进行调查。这个想法是制作一个可以接受任何实体的泛型方法。这是我们目前所拥有的;
班次结束 API 控制器:
public class EndOfShiftsController : ApiController
{
private iSuiteEntities db = new iSuiteEntities();
// POST: api/EndOfShifts/post
[Route("api/EndOfShift/post")]
[ResponseType(typeof(EndOfShift))]
public async Task<IHttpActionResult> PostEndOfShift_Post(EndOfShiftDto endOfShift)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
EndOfShift endofshift = new EndOfShift()
{
EndOfShiftID = endOfShift.EndOfShiftID,
EndOfShiftDate = endOfShift.EndOfShiftDate,
EquipmentID = endOfShift.EquipmentID,
StartHours = endOfShift.StartHours,
EndHours = endOfShift.EndHours,
CreatedBy = endOfShift.CreatedBy,
CreateDate = DateTime.Now,
Active = true
};
try
{
if (!endOfShift.ToDelete)
{
if (EndOfShiftExists(endOfShift.EndOfShiftID))
{
var update = db.EndOfShifts.Find(endOfShift.EndOfShiftID);
update.EndOfShiftDate = endofshift.EndOfShiftDate;
update.EquipmentID = endofshift.EquipmentID;
update.StartHours = endOfShift.StartHours;
update.EndHours = endOfShift.EndHours;
db.Entry(update).State = EntityState.Modified;
//try calling the audit log method here/////////////////////////////////
AuditsController.GetAuditLogData(endofshift, endOfShift.EndOfShiftID, endOfShift.CreatedBy);
await db.SaveChangesAsync();
}
else
{
db.EndOfShifts.Add(endofshift);
await db.SaveChangesAsync();
}
}
else
{
EndOfShift delete = db.EndOfShifts.Find(endOfShift.EndOfShiftID);
if (delete == null)
{
return NotFound();
}
else
{
delete.Active = false;
db.Entry(delete).State = EntityState.Modified;
await db.SaveChangesAsync();
}
}
return Ok("Success");
}
catch(Exception ex)
{
return Ok(ex.Message);
}
//return CreatedAtRoute("DefaultApi", new { id = endOfShift.EndOfShiftID }, endOfShift);
}
通用审核日志记录方法如下所示:
public class AuditsController : ApiController
{
private static iSuiteEntities dbContext = new iSuiteEntities();
private static iSuiteEntities db = new iSuiteEntities();
//write Audit log
public static async void GetAuditLogData<T>(T entity, int recID, int modByID) where T : new ()
{
try
{
var changeTrack = dbContext.ChangeTracker.Entries().Where(p => p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified);
foreach (var entry in changeTrack)
{
if (entry.Entity != null)
{
string entityName = string.Empty;
string state = string.Empty;
switch (entry.State)
{
case EntityState.Modified:
entityName = ObjectContext.GetObjectType(entry.Entity.GetType()).Name;
state = entry.State.ToString();
foreach (string prop in entry.OriginalValues.PropertyNames)
{
object currentValue = entry.CurrentValues[prop];
object originalValue = entry.OriginalValues[prop];
if (!currentValue.Equals(originalValue))
{
Audit auditEntry = new Audit()
{
recordID = recID,
tableName = entityName,
fieldName = prop,
oldValue = Convert.ToString(originalValue),
editReason = "Update",
modifiedBy = modByID,
modifiedDate = DateTime.Now
};
db.Audits.Add(auditEntry);
await db.SaveChangesAsync();
}
}
break;
//data addition is not required to be logged/////
case EntityState.Deleted:
entityName = ObjectContext.GetObjectType(entry.Entity.GetType()).Name;
state = entry.State.ToString();
foreach (string prop in entry.OriginalValues.PropertyNames)
{
Audit auditEntry = new Audit()
{
recordID = recID,
tableName = entityName,
fieldName = prop,
oldValue = Convert.ToString(entry.OriginalValues[prop]),
editReason = "Delete",
modifiedBy = modByID,
modifiedDate = DateTime.Now
};
db.Audits.Add(auditEntry);
await db.SaveChangesAsync();
}
break;
default:
break;
}
}
}
}
catch (Exception ex)
{
//handle exception here....
}
}
对于我们来说,这是否是我们追求的审计日志记录要求的可行选择?代码尚未经过测试,因此我确信我们也需要一些明智的指针编码。如果这不是实现我们目标的可行选项,您能否推荐一个可以使用数据库优先方法实现的选项?提前感谢!!
在对谷歌搜索采取了不同的方法之后,我们发现了这个链接,它为我们提供了解决问题的方法。把这个留在这里,希望能为其他开发人员节省一些时间:-(