"İçerik, model oluşturulurken kullanılamaz." web sayfamda web sayfamdan birinde sorun. Bu belirli web sayfası, ekranı yenilemek için her 2-3 saniyede bir sunucuya POST yapar. Testlerimden, bu sayfaya açılan 2 veya daha fazla tarayıcı örneğim varsa, birkaç dakika geçtikten sonra "İçerik oluşturulduğunda içerik kullanılamaz" istisnasının, depodaki derinlikten istisna olduğunu fark ettim.EF - HTTP istekleri sırasında model oluşturulduğu sırada içerik kullanılamıyor
Bu kod, gerekli verileri almak için bir "hizmet" çağırır. Bu kod, MVC Denetleyici sınıfının özel bir yetkilendirme özniteliğinde yürütülür. İşte
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;
}
depo var .... hata
oluşur: Burada
// 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
İşte
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; }
}
yukarıdaki hizmet çağrısı kodudur "RoomStationModel" dir
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);
}
}
Hatayı ekran görüntüsü:
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 Versiyon: 4.1 (Kod önce)
Bu görünüyor
yerine HttpContext.Current.Cache kullanıldı. Kodunuz gerçekten kötü bir şey yapıyor çünkü normalde model, içerik ilk kez kullanıldığında sadece bir kez oluşturuluyor. Uygulamanızın her istek sonrası uygulama havuzunu geri dönüştürmediğinden emin misiniz? –
Buna inanmıyorum, her bir yenilemeden sonra uygulama havuzunu geri dönüştürüp dönüştürmediğini nasıl anlarım? Bu bir IIS meselesi mi yoksa kodun içinde bir yer mi? – contactmatt
Bulduğum bir şey ilginçti, bu hata yalnızca denetleyicimde özel yetkilendirme özelliğini kullandığımda ortaya çıkıyor. Özel yetkilendirmeyi kaldırdığımda hata gider. – contactmatt