我有一个方法调用如下生成异常方法未找到:'System.Data.EntityState System.Data.Entity.Infrastructure.DbEntityEntry.get_State()当我调用dataRepository.Update(platformUser); :
public void LoginDelete(string aUserName) { MembershipUser loginToDelete = Membership.GetUser(aUserName); if (loginToDelete != null) { using (DataRepository dataRepository = DataRepository.Instance()) { using (TransactionScope transaction = dataRepository.Transaction()) { string userMembershipId = loginToDelete.ProviderUserKey.ToString(); PlatformUser platformUser = dataRepository.SingleOrDefault<PlatformUser>(r => r.MembershipUserId == userMembershipId); if (platformUser != null) { platformUser.MembershipUserId = ""; dataRepository.Update<PlatformUser>(platformUser); dataRepository.Save(); } Membership.DeleteUser(aUserName); transaction.Complete(); } }我的数据存储库定义如下:
public class EntityFrameworkRepository : IRepository { private readonly DbContext _DataContext; public EntityFrameworkRepository(DbContext aDataContext) { _DataContext = aDataContext; } public int Save() { return _DataContext.SaveChanges(); } public T SingleOrDefault<T>(Expression<Func<T, bool>> aPredicate) where T : class { return _DataContext.Set<T>() .SingleOrDefault(aPredicate); } public TransactionScope Transaction(TransactionScopeOption aTransactionScopeOption = TransactionScopeOption.RequiresNew, IsolationLevel aIsolationLevel = IsolationLevel.ReadUncommitted) { return new TransactionScope(aTransactionScopeOption, new TransactionOptions { IsolationLevel = aIsolationLevel } ); } public void Update<T>(T aEntity) where T : class { DbEntityEntry entityEntry = _DataContext.Entry(aEntity); if (entityEntry.State == EntityState.Detached) { _DataContext.Set<T>() .Attach(aEntity); entityEntry.State = EntityState.Modified; } } public DbContext DataContext { get { return _DataContext; } } } public class DataRepository : EntityFrameworkRepository, IDisposable, IDataRepository { public DataRepository() : base(new DataContext()) { } public static DataRepository Instance() { return new DataRepository(); } }有人可以向我解释为什么我得到例外以及我如何解决它?
VS2012 EF 6.0.2I have a method call as follows which generates the exception Method not found: 'System.Data.EntityState System.Data.Entity.Infrastructure.DbEntityEntry.get_State() when I make the call dataRepository.Update(platformUser);:
public void LoginDelete(string aUserName) { MembershipUser loginToDelete = Membership.GetUser(aUserName); if (loginToDelete != null) { using (DataRepository dataRepository = DataRepository.Instance()) { using (TransactionScope transaction = dataRepository.Transaction()) { string userMembershipId = loginToDelete.ProviderUserKey.ToString(); PlatformUser platformUser = dataRepository.SingleOrDefault<PlatformUser>(r => r.MembershipUserId == userMembershipId); if (platformUser != null) { platformUser.MembershipUserId = ""; dataRepository.Update<PlatformUser>(platformUser); dataRepository.Save(); } Membership.DeleteUser(aUserName); transaction.Complete(); } }and my data repository is defined as follows:
public class EntityFrameworkRepository : IRepository { private readonly DbContext _DataContext; public EntityFrameworkRepository(DbContext aDataContext) { _DataContext = aDataContext; } public int Save() { return _DataContext.SaveChanges(); } public T SingleOrDefault<T>(Expression<Func<T, bool>> aPredicate) where T : class { return _DataContext.Set<T>() .SingleOrDefault(aPredicate); } public TransactionScope Transaction(TransactionScopeOption aTransactionScopeOption = TransactionScopeOption.RequiresNew, IsolationLevel aIsolationLevel = IsolationLevel.ReadUncommitted) { return new TransactionScope(aTransactionScopeOption, new TransactionOptions { IsolationLevel = aIsolationLevel } ); } public void Update<T>(T aEntity) where T : class { DbEntityEntry entityEntry = _DataContext.Entry(aEntity); if (entityEntry.State == EntityState.Detached) { _DataContext.Set<T>() .Attach(aEntity); entityEntry.State = EntityState.Modified; } } public DbContext DataContext { get { return _DataContext; } } } public class DataRepository : EntityFrameworkRepository, IDisposable, IDataRepository { public DataRepository() : base(new DataContext()) { } public static DataRepository Instance() { return new DataRepository(); } }Can someone explain to me why I am getting the exception and how I fix it?
最满意答案
看来我仍然有一个旧的EF 5浮动参考,这引起了问题。 一旦我确定这也是EF 6.0.2一切都很好。
It appears I still had an old reference to EF 5 floating around which was causing the issue. Once I made sure that was also EF 6.0.2 all was well.
更多推荐
发布评论