找不到方法DbEntityEntry.get

编程入门 行业动态 更新时间:2024-10-25 08:20:52
找不到方法DbEntityEntry.get_State()(Method not found DbEntityEntry.get_State()) 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(); } }

有人可以向我解释为什么我得到例外以及我如何解决它?

VS2012 EF 6.0.2

I 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.

更多推荐

本文发布于:2023-08-06 02:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   方法   DbEntityEntry

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!