0

我正在使用 Simple Membership 和一个维护 UserId 和 UserName 的 UserProfile 表:

 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 partial class UowContext : DbContext

    // code to set up DbSets here ...
    public DbSet<Content> Contents { get; set; }

    private void ApplyRules()
    {
        var r1 = new Random();
        var r2 = new Random();

        foreach (var entry in this.ChangeTracker.Entries()
                     .Where(
                          e => e.Entity is IAuditableTable &&
                         (e.State == EntityState.Added) ||
                         (e.State == EntityState.Modified)))
        {
            IAuditableTable e = (IAuditableTable)entry.Entity;
            if (entry.State == EntityState.Added)
            {
                e.CreatedBy = // I want to put the integer value of UserId here
                e.CreatedDate = DateTime.Now;
            }
            e.ModifiedBy = // I want to put the integer value of UserId here
            e.ModifiedDate = DateTime.Now;
        }
    }

这是显示如何存储用户信息的模式。请注意,我将整数 UserId 而不是 UserName 存储在表中:

public abstract class AuditableTable : IAuditableTable
{
    public virtual byte[] Version { get; set; }
    public int CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public int ModifiedBy { get; set; }
    public DateTime ModifiedDate { 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 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);
        }
    }

如何从 Context 内部确定 UserId,以便在表中填充 Id?

4

1 回答 1

0

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

HttpContext.Current.User.Identity.Name

您可以针对该名称查询UserProfile表并从那里获取 UserId,然后将其分配给 ModifiedBy 属性。

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

于 2013-07-09T13:25:18.370 回答