I have this view:
@model MatchGaming.Models.ProfileQuery @{ ViewBag.Title = "Index"; } <h2>Index</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm("Results", "Profiles")) { @Html.ValidationSummary(true) <fieldset> <legend>ProfileQuery</legend> @Html.EditorFor(model=>model.SearchString) <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>
I have this controller for the HttpPost:
[HttpPost] public ActionResult Results(ProfileQuery profileQuery) { Debug.Write(profileQuery.SearchString); using(var db = new MatchGamingEntities()) { var SearchUserName = db.Users.SingleOrDefault(a=> a.UserName.Contains(profileQuery.SearchString)); var Users = from m in db.Users join m2 in db.MyProfiles on m.UserId equals m2.UserId where m.UserName == SearchUserName.UserName select new UserViewModel { UserName = m.UserName, LastActivityDate = m.LastActivityDate, Address = m2.Address, City = m2.City, State = m2.State, Zip = m2.Zip }; return View(Users.AsEnumerable()); } }
Here is the View for Results:
@model IEnumerable<MatchGaming.Models.UserViewModel> @{ ViewBag.Title = "Results"; } <h2>Results</h2> <fieldset> <legend>UserViewModel</legend> @foreach (var item in Model){ <div class="display-label">UserName</div> <div class="display-field">@item.UserName</div> <div class="display-label">LastActivityDate</div> <div class="display-field">@String.Format("{0:g}", item.LastActivityDate)</div> <div class="display-label">Address</div> <div class="display-field">@item.Address</div> <div class="display-label">City</div> <div class="display-field">@item.City</div> <div class="display-label">State</div> <div class="display-field">@item.State</div> <div class="display-label">Zip</div> <div class="display-field">@item.Zip</div> } </fieldset>
I keep getting this error:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I can't figure out why.
The ObjectContext class is the primary class for interacting with data as objects that are instances of entity types that are defined in a conceptual model. An instance of the ObjectContext class encapsulates the following: A connection to the database, in the form of an EntityConnection object.
This can be achieved in several ways: setting the EntityState for the entity explicitly; using the DbContext. Update method (which is new in EF Core); using the DbContext. Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.
It discovers entity sets based on DbSet properties defined on the DbContext derived class (or in general, it discovers your model based on your code). ObjectContext does not do any discovery and is not convention based. It just reads your model from csdl, ssdl and msl artifacts.
DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table. Your code sample doesn't fit the expected pattern.
I am guessing that the problem is that the execution of your LINQ query has been deferred until starting to access them on your view. At this point db
has already been disposed.
Try this:
return View(Users.ToList());
Added ToList()
That will force the fetch from the DB before disposing db
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With