我在EF6和aspnet Identity中构建项目。
我遇到以下问题:
如果我打电话
var account = await FindByNameAsync(userName); // account.IsConfirmed = true
我找到了我正在寻找的帐户(例如:isConfirmed = true)。
当我手动更改数据库中的值(isConfirmed = true - > isConfirmed = false)并再次运行我的查询时,我仍然得到我的旧帐户对象(isConfirmed = true)
var account = await FindByNameAsync(userName); // Should be account.IsConfirmed = false, but still gives me IsConfirmed = true
我尝试在我的DbContext构造函数中添加以下内容
> this.Configuration.ProxyCreationEnabled = false;
> this.Configuration.LazyLoadingEnabled = false;
但这并没有改变任何事情。
我该怎么办?缓存数据保留多长时间? 我见过的所有帖子都要求你运行查询(来自......),但看看我是如何使用aspnet身份而我无法控制这些事情的,我该怎么办?
谢谢!
编辑:添加了dbContext信息
我的IoC(Unity)
container.RegisterType<IUnitOfWork, UserManagementContext>(new HttpContextLifetimeManager<IUnitOfWork>());
container.RegisterType<IUserStore<Account>, UserStore<Account>>(new InjectionConstructor(container.Resolve<IUnitOfWork>()));
HttpContextLifeTimeManager:
public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable
{
public override object GetValue()
{
return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
}
public override void SetValue(object newValue)
{
HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue;
}
public override void RemoveValue()
{
HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
}
public void Dispose()
{
RemoveValue();
}
}
我的IUnitOfWork
public interface IUnitOfWork : IDisposable
{
void Save();
Task SaveAsync();
DbSet<TEntity> EntitySet<TEntity>() where TEntity : class;
void MarkAsModified<TEntity>(TEntity entity) where TEntity : class;
}
我的UserManagementContext
public class UserManagementContext : IdentityDbContext<Account>, IUnitOfWork
{
static UserManagementContext()
{
//Database.SetInitializer<UserManagementContext>(new RecreateDatabase());
Database.SetInitializer<UserManagementContext>(null);
}
public UserManagementContext()
: base("Name=UserManagementConnection")
{
this.Configuration.ProxyCreationEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
}
// ... (my Dbsets)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// configuration ..
}
public void Save()
{
SaveChanges();
}
public async Task SaveAsync()
{
await SaveChangesAsync();
}
public DbSet<TEntity> EntitySet<TEntity>() where TEntity : class
{
return this.Set<TEntity>();
}
public void MarkAsModified<TEntity>(TEntity entity) where TEntity : class
{
this.Entry(entity).State = EntityState.Modified;
}
}
更新
我发现了另一件奇怪的事情。当我设置我的上次登录日期字段时,该更改会被选中,但是当我设置我的isConfirmed字段时,它不会被拾取..(数据库更改实际上会被缓存的数据覆盖!
因此,这确认了通过代码输入的数据得以保留,但数据库中的手动更改将被忽略。
更新2 如果有人也有这个问题:问题不是aspnet身份,它是EF。
我所做的是实现我自己的用户界面并手动访问EF并使用.AsNoTracking()来避免缓存。
答案 0 :(得分:3)
HttpContext.Current在异步编程中是邪恶的。
同步或早期代码只能执行一个上下文的方法和每个线程一个控制器。所以没有冲突。
在异步编程中,多个控制器实例的方法在同一个线程上执行。所以HttpContext.Current的值与你想的不一样,很脏!!!
相反,您应该保留您的HttpContext并在异步代码中使用它,如下所示。
public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable
{
private HttpContext Context;
public HttpContextLifetimeManager(HttpContext context){
this.Context = context;
}
public override object GetValue()
{
return Context.Items[typeof(T).AssemblyQualifiedName];
}
public override void SetValue(object newValue)
{
Context.Items[typeof(T).AssemblyQualifiedName] = newValue;
}
public override void RemoveValue()
{
Context.Items.Remove(typeof(T).AssemblyQualifiedName);
}
public void Dispose()
{
RemoveValue();
}
}
container.RegisterType<IUnitOfWork, UserManagementContext>(
new HttpContextLifetimeManager<IUnitOfWork>(this.ControllerContext.HttpContext));
普通旧继承
我建议使用抽象实体控制器模式,它在异步模式下很容易使用。
public abstract class EntityController<TDbContext> : Controller
where TDbContext: DbContext
{
protected TDbContext DB { get; private set;}
public EntityController(){
DB = Activator.CreateInstance<TDbContext>();
}
protected override void OnDispose(){
DB.Dispose();
}
}
相应地导出你的控制器,
public class UserController : EntityController<UserManagementContext>
{
public async Task<ActionResult> SomeMethod(){
......
var user = await DB.FindByNameAsync(userName);
......
}
}
如果您仍想使用Unity,则必须为每个请求创建新的统一实例,但这只是浪费CPU周期。在我看来,在MVC中使用Unity来实现更简单的任务就是编程。如果用抽象类很容易完成的事情。异步编程有很多新东西,Unity并不是为此而设计的。