I'm stucked with this problem and i've spent more than 3 hours scouring the web and SO for a solution to no avail. My Code keeps throwing this error
The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
This is my Context
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<ApprovalStatus> ApprovalStatuses { get; set; }
public DbSet<Bank> Banks { get; set; }
public DbSet<Documents> Documents { get; set; }
public DbSet<EducationLevel> EducationLevel { get; set; }
public DbSet<EmploymentDetail> EmploymentDetails { get; set; }
public DbSet<EmploymentStatus> EmploymentStatus { get; set; }
public DbSet<Gender> Genders { get; set; }
public DbSet<GuestPersonalDetail> GuestPersonalDetails { get; set; }
public DbSet<LGA> LocalGovts { get; set; }
public DbSet<LoanDetail> LoanDetails { get; set; }
public DbSet<MaritalStatus> MaritalStatuses { get; set; }
public DbSet<NameTitle> NameTitles { get; set; }
public DbSet<NextOfKin> NextOfKins { get; set; }
public DbSet<RejectionReasons> RejectionReasons { get; set; }
public DbSet<RepaymentMode> RepaymentModes { get; set; }
}
in my controller i have this where i try to populate a dropdownlist in my view
[HttpGet]
public ActionResult Apply()
{
var db = new ApplicationDbContext();
ApplicationVM model = new ApplicationVM();
model.BankList = new SelectList(db.Banks, "Bankid", "BankName");
model.EducationLevellist = new SelectList(db.EducationLevel, "EducationLevelid", "Level");
model.EmploymentStatuslist = new SelectList(db.EmploymentStatus, "EmploymentStatusid", "EmpStatus");
model.GenderList = new SelectList(db.Genders, "Genderid", "GenderVal");
model.LGAlist = new SelectList(db.LocalGovts, "LGAid", "LgName");
model.MaritalStatusList = new SelectList(db.MaritalStatuses, "MaritalStatusid", "Status");
model.NameTitleList = new SelectList(db.NameTitles, "NameTitleid", "Title");
model.RepaymentModeList = new SelectList(db.RepaymentModes, "RepaymentModeid", "ModeOfRepayment");
return View(model);
}
i used this in view like this
<div class="form-group">
@Html.LabelFor(model => model.ApplicantTitleid, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.ApplicantTitleid, Model.NameTitleList, null, String.Empty)
@Html.ValidationMessageFor(model => model.ApplicantTitleid)
</div>
</div>
The error is being thrown at the Dropdownlistfor point. This is the stack trace
[InvalidOperationException: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.]
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +115
System.Data.Entity.Internal.InternalContext.Initialize() +31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +138
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +38
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +108
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +157
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +133
System.Web.Mvc.MultiSelectList.GetListItemsWithValueField() +461
System.Web.Mvc.MultiSelectList.GetListItems() +89
System.Web.Mvc.MultiSelectList.GetEnumerator() +39
System.Web.Mvc.Html.SelectExtensions.GetSelectListWithDefaultValue(IEnumerable`1 selectList, Object defaultValue, Boolean allowMultiple) +557
System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, ModelMetadata metadata, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes) +574
System.Web.Mvc.Html.SelectExtensions.DropDownListHelper(HtmlHelper htmlHelper, ModelMetadata metadata, String expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes) +56
System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes) +233
System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, Object htmlAttributes) +142
ASP._Page_Views_Loan_Apply_cshtml.Execute() in c:\Users\RIDWAN\Documents\visual studio 2013\projects\ApplicationPortal\ApplicationPortal\Views\Loan\Apply.cshtml:37
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +120
System.Web.WebPages.StartPage.RunPage() +63
System.Web.WebPages.StartPage.ExecutePageHierarchy() +100
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +131
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
System.Web.Mvc.Async.<>c__DisplayClass28.<BeginInvokeAction>b__19() +321
System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +932
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +188
What am i doing wrong exactly?