"모델을 만드는 동안 컨텍스트를 사용할 수 없습니다."라는 메시지가 나타납니다. 내 웹 페이지 중 하나에서 내 웹 응용 프로그램의 문제. 이 특정 웹 페이지는 2-3 초마다 서버에 POST되어 화면을 새로 고칩니다. 내 테스트에서 나는이 페이지에 2 개 이상의 브라우저 인스턴스가 열려있는 경우 몇 분 후에 리포지토리의 깊은 곳에서 "모델을 만드는 동안 컨텍스트를 사용할 수 없습니다"라는 메시지가 나타납니다.EF - HTTP 요청 중에 모델을 만들 때 컨텍스트를 사용할 수 없습니다.
이 코드는 필요한 데이터를 검색하기 위해 "서비스"를 호출합니다. 이 코드는 MVC Controller 클래스의 사용자 정의 권한 특성에서 실행됩니다. 여기
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;
}
저장소입니다 .... 오류가
발생 : 여기
// 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
여기
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; }
}
위의 서비스 호출에 대한 코드 인 "RoomStationModel"입니다
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);
}
}
오류의 스크린 샷 :
스택 트레이스 :
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 버전 : 4.1 (코드 첫 번째)
이 보인다
이런 일은 일어나지 않습니다. 일반적으로 컨텍스트가 처음 사용될 때 모델이 한 번만 생성되기 때문에 코드가 실제로 잘못된 것입니다. 요청이있을 때마다 응용 프로그램이 응용 프로그램 풀을 재활용하지 않는 것이 확실합니까? –
믿을 수 없다. 새로 고침 후 앱 풀을 재활용하면 어떻게 알 수 있습니까? 그게 IIS 일 것인가 아니면 코드 어딘가에 있을까요? – contactmatt
재미있는 점 중 하나는 내 컨트롤러에서 사용자 지정 인증 특성을 사용할 때만이 오류가 발생한다는 것입니다. 맞춤 인증을 제거하면 오류가 사라집니다. – contactmatt