2014-01-15 99 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,一切都很好。