Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 在实体框架4中,如何在保存时自动更新具有UserId的实体的int ModifiedBy属性?_Sql Server_Asp.net Mvc_Entity Framework_Asp.net Mvc 4_Entity Framework 4 - Fatal编程技术网

Sql server 在实体框架4中,如何在保存时自动更新具有UserId的实体的int ModifiedBy属性?

Sql server 在实体框架4中,如何在保存时自动更新具有UserId的实体的int ModifiedBy属性?,sql-server,asp.net-mvc,entity-framework,asp.net-mvc-4,entity-framework-4,Sql Server,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Entity Framework 4,我使用的是简单的成员资格和一个UserProfile表,该表维护UserId和UserName: public partial class UserProfile { public UserProfile() { this.webpages_Roles = new List<webpages_Roles>(); } public int UserId { get; set; }

我使用的是简单的成员资格和一个UserProfile表,该表维护UserId和UserName:

 public partial class UserProfile
    {
        public UserProfile()
        {
            this.webpages_Roles = new List<webpages_Roles>();
        }

        public int UserId { get; set; }
        public string UserName { get; set; }
        public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
    }
以下是我使用的控制器操作示例:

public HttpResponseMessage PostContent(Content content)
    {
        try
        {
            _uow.Contents.Add(content);
            _uow.Commit();
            var response = Request.CreateResponse<Content>(HttpStatusCode.Created, content);
            return response;
        }
        catch (DbUpdateException ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.Conflict, ex);
        } 
    }
public HttpResponseMessage PostContent(内容)
{
尝试
{
_uow.Contents.Add(内容);
_提交();
var response=Request.CreateResponse(HttpStatusCode.Created,content);
返回响应;
}
捕获(DbUpdateException ex)
{
返回请求.CreateErrorResponse(HttpStatusCode.Conflict,ex);
} 
}
然后我有:

public class UowBase : IUow, IDisposable
{
    public UowBase(IRepositoryProvider repositoryProvider)
    {
        CreateDbContext();

        repositoryProvider.DbContext = DbContext;
        RepositoryProvider = repositoryProvider;
    }

    public IRepository<Content> Contents { get { return GetStandardRepo<Content>(); } }
公共类UowBase:IUow,IDisposable
{
公共UowBase(IRepositoryProvider repositoryProvider)
{
CreateDbContext();
repositoryProvider.DbContext=DbContext;
RepositoryProvider=RepositoryProvider;
}
公共IRepository内容{get{return GetStandardRepo();}}
以及:

公共类GenericRepository:IRepository,其中T:class
{
公共通用存储库(DbContext DbContext)
{
if(dbContext==null)
抛出新ArgumentNullException(“使用此存储库需要DbContext的实例”、“上下文”);
DbContext=DbContext;
DbSet=DbContext.Set();
}
公共虚拟空添加(T实体)
{
DbEntityEntry DbEntityEntry=DbContext.Entry(实体);
if(dbEntityEntry.State!=EntityState.Distached)
{
dbEntityEntry.State=EntityState.Added;
}
其他的
{
添加(实体);
}
}

如何从我的上下文中确定UserId,以便在我的表中填充该Id?

在代码中,您将通过以下方式获得UserName:

HttpContext.Current.User.Identity.Name
您可以根据该名称查询
UserProfile
表,并从中获取UserId,然后将其分配给ModifiedBy属性


确保您在foreach循环之外查询UserProfile表:)

检查链接,但该问题会用名称填充ModifiedBy,并且我的数据库会存储用户ID。我将更新我的问题以使其更清楚。您是否使用DbContext?您在何处创建和使用上下文?在控制器操作中?中的voking代码看起来像?是的,上面的代码在里面:公共部分类UowContext:DbContextYour
UowContext
应该有一个属性“UserId”,并且应该在控制器中填充它(理想情况下通过DI)。
public class UowBase : IUow, IDisposable
{
    public UowBase(IRepositoryProvider repositoryProvider)
    {
        CreateDbContext();

        repositoryProvider.DbContext = DbContext;
        RepositoryProvider = repositoryProvider;
    }

    public IRepository<Content> Contents { get { return GetStandardRepo<Content>(); } }
public class GenericRepository<T> : IRepository<T> where T : class
{
    public GenericRepository(DbContext dbContext)
    {
        if (dbContext == null) 
            throw new ArgumentNullException("An instance of DbContext is required to use this repository", "context");
        DbContext = dbContext;
        DbSet = DbContext.Set<T>();
    }

    public virtual void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
    }
HttpContext.Current.User.Identity.Name