{"action":"created","issue":{"url":"https://api.github.com/repos/aspnetboilerplate/aspnetboilerplate/issues/361","labels_url":"https://api.github.com/repos/aspnetboilerplate/aspnetboilerplate/issues/361/labels{/name}","comments_url":"https://api.github.com/repos/aspnetboilerplate/aspnetboilerplate/issues/361/comments","events_url":"https://api.github.com/repos/aspnetboilerplate/aspnetboilerplate/issues/361/events","html_url":"https://github.com/aspnetboilerplate/aspnetboilerplate/issues/361","id":59298150,"number":361,"title":"Exception while accessing Repository Item","user":{"login":"tbitowt","id":6546880,"avatar_url":"https://avatars.githubusercontent.com/u/6546880?v=3","gravatar_id":"","url":"https://api.github.com/users/tbitowt","html_url":"https://github.com/tbitowt","followers_url":"https://api.github.com/users/tbitowt/followers","following_url":"https://api.github.com/users/tbitowt/following{/other_user}","gists_url":"https://api.github.com/users/tbitowt/gists{/gist_id}","starred_url":"https://api.github.com/users/tbitowt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tbitowt/subscriptions","organizations_url":"https://api.github.com/users/tbitowt/orgs","repos_url":"https://api.github.com/users/tbitowt/repos","events_url":"https://api.github.com/users/tbitowt/events{/privacy}","received_events_url":"https://api.github.com/users/tbitowt/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnetboilerplate/aspnetboilerplate/labels/question","name":"question","color":"bbbbbb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-02-27T21:39:10Z","updated_at":"2015-02-28T14:28:09Z","closed_at":null,"body":"Hi!\r\n\r\nI'm fighting with a simple project based on your framework. I have Created 2 models:\r\n\r\n    public class Comment : Entity<int>\r\n    {\r\n        [DataType(DataType.MultilineText)]\r\n        public string Text { get; set; }\r\n        public string Author { get; set; }\r\n\r\n        public int ItemID { get; set; }\r\n        public virtual Item Item { get; set; }\r\n    }\r\n    public class Item : Entity<int>\r\n    {\r\n        public string Title { get; set; }\r\n\r\n        public string Description { get; set; }\r\n\r\n        public ItemSourceType SourceType { get; set; }\r\n\r\n        public byte[] PhotoBytes { get; set; }\r\n\r\n        public string Url { get; set; }\r\n\r\n        public virtual ICollection<Comment> Comments { get; set; }\r\n    }\r\n\r\nAdditionally I have Repository:\r\n\r\n    public class ItemRepository : KwejkAppRepositoryBase<Item>\r\n    {\r\n        public ItemRepository(IDbContextProvider<KwejkAppDbContext> dbContextProvider) : base(dbContextProvider)\r\n        {\r\n        }\r\n    }\r\n\r\nAnd in Controller I'm trying to get All Items by: (don't look at logic... It doesn't matter)\r\n        public ActionResult Details(int? id)\r\n        {\r\n            if (id == null)\r\n            {\r\n                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);\r\n            }\r\n\r\n            var queryable = _repoItems.GetAll().ToList();\r\n            Item item = null;\r\n            if (item == null)\r\n            {\r\n                return HttpNotFound();\r\n            }\r\n            return View(item);\r\n        }\r\n\r\nI'm instantiting repository by DI (injecting it into Controller's contructor)\r\n\r\n        public ItemsController(ItemRepository repoItems)\r\n        {\r\n            this._repoItems = repoItems;\r\n        }\r\n\r\nThe point is that I'm getting exception:\r\n```\r\nAn exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code\r\n\r\nAdditional information: The operation cannot be completed because the DbContext has been disposed.\r\n```\r\n\r\nI have updated all Abp libraries to newest.\r\nI'm using EntityFramework as ORM.\r\n"},"comment":{"url":"https://api.github.com/repos/aspnetboilerplate/aspnetboilerplate/issues/comments/76528089","html_url":"https://github.com/aspnetboilerplate/aspnetboilerplate/issues/361#issuecomment-76528089","issue_url":"https://api.github.com/repos/aspnetboilerplate/aspnetboilerplate/issues/361","id":76528089,"user":{"login":"hikalkan","id":1210527,"avatar_url":"https://avatars.githubusercontent.com/u/1210527?v=3","gravatar_id":"","url":"https://api.github.com/users/hikalkan","html_url":"https://github.com/hikalkan","followers_url":"https://api.github.com/users/hikalkan/followers","following_url":"https://api.github.com/users/hikalkan/following{/other_user}","gists_url":"https://api.github.com/users/hikalkan/gists{/gist_id}","starred_url":"https://api.github.com/users/hikalkan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hikalkan/subscriptions","organizations_url":"https://api.github.com/users/hikalkan/orgs","repos_url":"https://api.github.com/users/hikalkan/repos","events_url":"https://api.github.com/users/hikalkan/events{/privacy}","received_events_url":"https://api.github.com/users/hikalkan/received_events","type":"User","site_admin":false},"created_at":"2015-02-28T14:28:09Z","updated_at":"2015-02-28T14:28:09Z","body":"No, methods should be virtual and UOW only if they need open database connection in the method body (GetAll() returns IQueryable and it needs an open DB connection when you call ToList() on it).\r\nYes, you can set it to all controller methods and make they virtual. But it's needless.\r\n\r\nSee docs: http://www.aspnetboilerplate.com/Pages/Documents/Repositories#DocGettingListOfEntities\r\n\r\nAbout IQueryable<T>\r\nWhen you call GetAll() out of a repository method, there must be an open database connection. This is because of deferred execution of IQueryable<T>. It does not perform database query unless you call ToList() method or use the IQueryable<T> in a foreach loop (or somehow access to queried items). So, when you call ToList() method, database connection must be alive. This can be achived by marking caller method by UnitOfWork attribute of ASP.NET Boilerplate. Note that Application Service methods are already UnitOfWork as default, so, GetAll() will work without adding UnitOfWork attribute for application service methods.\r\n\r\n\r\n\r\n\r\n\r\n"}}