找不到方法DbEntityEntry.get_State()

时间:2014-01-15 06:37:44

标签: c# entity-framework

  • VS2012
  • EF 6.0.2

我有一个方法调用如下,它生成异常找不到的方法:'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();
    }

} 

有人可以向我解释为什么我会得到异常以及我如何修复它?

1 个答案:

答案 0 :(得分:1)

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