2014-01-15 70 views
0
  • VS2012
  • EF 6.0.2

我有一个方法调用如下产生异常未找到方法:“System.Data.EntityState系统.Data.Entity.Infrastructure.DbEntityEntry.get_State()当我拨打电话dataRepository.Update(platformUser);未找到方法DbEntityEntry.get_State()

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(); 
    } 

} 

为什么我收到的异常可以有人向我解释,我应该如何解决?

回答

1

看来我仍然有一个EF 5浮动引起的问题的旧参考。一旦我确定这也是EF 6.0.2,一切都很好。