Tôi nhận được "Không thể sử dụng ngữ cảnh trong khi tạo mô hình". vấn đề trong ứng dụng web của tôi ở một trong các trang web của tôi. Trang web cụ thể này POSTs tới máy chủ sau mỗi 2-3 giây để làm mới màn hình. Từ thử nghiệm của tôi, tôi thấy rằng nếu tôi có từ 2 trường hợp trình duyệt trở lên mở trang này, sau vài phút, tôi nhận được "Không thể sử dụng ngữ cảnh trong khi mô hình đang được tạo" ngoại trừ từ sâu trong kho lưu trữ.EF - Không thể sử dụng ngữ cảnh trong khi mô hình đang được tạo ngoại lệ trong các yêu cầu HTTP
Mã này gọi là "dịch vụ" để truy xuất dữ liệu cần thiết. Mã này được thực hiện trong một thuộc tính ủy quyền tùy chỉnh của lớp MVC Controller.
// Code in custom "Authorization" attribute on the controller
int? stationId = stationCookieValue; // Read value from cookie
RoomStationModel roomStationModel = RoomStationService.GetRoomStation(stationId); // Error occurs inside this call
Đây là "RoomStationModel"
public class RoomStationModel
{
[Key]
public int RoomStationId { get; set; }
public int? RoomId { get; set; }
[ForeignKey("RoomId")]
public virtual RoomModel Room { get; set; }
/* Some other data properties.... */
}
public class RoomModel
{
[Key]
public int RoomId { get; set; }
public virtual ICollection<RoomStationModel> Stations { get; set; }
}
Đây là mã cho các cuộc gọi dịch vụ trên:
public RoomStationModel GetRoomStation(int? roomStationId)
{
RoomStationModel roomStationModel = null;
if (roomStationId.HasValue)
{
using (IRepository<RoomStationModel> roomStationRepo = new Repository<RoomStationModel>(Context))
{
roomStationModel = roomStationRepo.FirstOrDefault(rs => rs.RoomStationId == roomStationId.Value, false, new string[] { "Room" });
}
}
return roomStationModel;
}
Đây là kho .... nơi lỗi xảy ra
public class Repository<TObject> : IRepository<TObject> where TObject : class
{
protected MyContext Context = null;
public Repository(IDataContext context)
{
Context = context as MyContext;
}
protected DbSet<TObject> DbSet { get { return Context.Set<TObject>(); } }
public virtual TObject FirstOrDefault(Expression<Func<TObject, bool>> predicate, bool track = true, string[] children = null)
{
var objectSet = DbSet.AsQueryable();
if (children != null)
foreach (string child in children)
objectSet = objectSet.Include(child);
if (track)
return objectSet.Where(predicate).FirstOrDefault<TObject>(predicate);
return objectSet.Where(predicate).AsNoTracking().FirstOrDefault<TObject>(predicate);
}
}
Ảnh chụp màn hình lỗi:
stacktrace:
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at System.Data.Entity.DbExtensions.Include[T](IQueryable`1 source, String path)
at Vanguard.AssetManager.Data.Repository`1.FirstOrDefault(Expression`1 predicate, Boolean track, String[] children) in C:\Work\VanguardAssetManager\Main\Vanguard.AssetManager.Data\Repository.cs:line 100
at Vanguard.AssetManager.Services.Business.RoomStationService.GetRoomStation(Nullable`1 roomStationId) in C:\Work\VanguardAssetManager\Main\Vanguard.AssetManager.Services\Business\RoomStationService.cs:line 61
at Vanguard.AssetManager.Web.Attributes.RoomStationAuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) in C:\Work\VanguardAssetManager\Main\Vanguard.AssetManager.Web\Attributes\RoomStationAuthorizeAttribute.cs:line 52
at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
EF Version: 4.1 (Mã đầu tiên)
Điều này không nên xảy ra. Mã của bạn đang làm một cái gì đó thực sự xấu bởi vì thông thường mô hình được tạo ra chỉ một lần khi bối cảnh được sử dụng lần đầu tiên. Bạn có chắc chắn rằng ứng dụng của bạn không tái chế hồ bơi ứng dụng sau mỗi yêu cầu? –
Tôi không tin đó là, làm sao tôi biết được liệu nó có tái chế hồ bơi ứng dụng sau mỗi lần làm mới không? Đó có phải là một điều IIS hoặc một nơi nào đó trong mã? – contactmatt
Một điều mà tôi thấy thú vị là lỗi này chỉ xảy ra khi tôi sử dụng thuộc tính ủy quyền tùy chỉnh trên bộ điều khiển của mình. Khi tôi xóa ủy quyền tùy chỉnh, lỗi sẽ biến mất. – contactmatt