在ObjectContext.SavingChanges当前用户身份 [英] Current user identity in ObjectContext.SavingChanges
问题描述
我使用 ObjectContext.SavingChanges
事件基于这样的对象状态更新CreatedDate / UpdatedDate列
I am using ObjectContext.SavingChanges
event to update CreatedDate/UpdatedDate columns based on the object state like this
partial void OnContextCreated()
{
//Extension of the Command Timeout to avoid processing problems.
CommandTimeout = 600; //Time in seconds
this.SavingChanges += new EventHandler(Entities_SavingChanges);
}
protected void Entities_SavingChanges(object sender, EventArgs e)
{
var addedObjects = this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added);
foreach (var addedObject in addedObjects)
{
var propertyInfo = addedObject.Entity.GetType().GetProperty("CreatedDate");
if (propertyInfo != null)
{
propertyInfo.SetValue(addedObject.Entity, DateTime.UtcNow, null);
}
}
var modifiedObjects = this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified);
foreach (var modifiedObject in modifiedObjects)
{
var propertyInfo = modifiedObject.Entity.GetType().GetProperty("UpdatedDate");
if (propertyInfo != null)
{
propertyInfo.SetValue(modifiedObject.Entity, DateTime.UtcNow, null);
}
}
}
我有两列CreatedUser和UpdatedUser。
I have two more columns CreatedUser and UpdatedUser.
有什么办法来更新从上下文中那些使用当前用户名?
Is there any way to update those using current user name from context?
Ofcource, System.HttpContext.Current
为空在这里,因为这是我的单独的类库项目,我从WCF服务访问。
Ofcource, System.HttpContext.Current
is null here as this is my separate class library project, which I access through WCF service.
推荐答案
如果这个code所在,你从你的ASP.NET MVC应用程序消耗WCF服务,你可以发送当前用户名作为参数传递给您的方法正在调用。
If this code resides in a WCF service that you consume from your ASP.NET MVC application you could send the current username as parameter to the method you are invoking.
这篇关于在ObjectContext.SavingChanges当前用户身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!