Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Odd LINQ behaviour - Works for One Record bu causing Fault on more than one

$
0
0

I have the following controller:

 public async Task<IActionResult> SiteJobDetails(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var job = await _context.Job
                .Include(m=>m.Site.Client)
                .Include(m=>m.Site.SiteType)
                .Include(m => m.WaterBody)
                .OrderBy(m=>m.BookingDate)
                .AsNoTracking()
                .SingleOrDefaultAsync(m => m.Site.SiteID == id);


            if (job == null)
            {
                return NotFound();
            }

            return View(job);
        }

Working with the following View:

@model EVA.Models.Job

@{
    ViewData["Title"] = "Details";
}<h2>@Html.DisplayFor(model => model.Site.SiteName) Details</h2><div><div class="col-md-10"><a asp-action="HealthDetails" asp-route-id="@Model.SiteID">Health View</a> | <a asp-action="AccessDetails" asp-route-id="@Model.SiteID">Access View</a>
        | <a asp-action="SiteTasksDetails" asp-route-id="@Model.SiteID">Task View</a></div><hr /><dl class="dl-horizontal"><dt>
            @Html.DisplayNameFor(model => model.Site.Client.Name)</dt><dd>
            @Html.DisplayFor(model => model.Site.Client.Name)</dd><dt>
            @Html.DisplayNameFor(model => model.Site.SiteName)</dt><dd>
            @Html.DisplayFor(model => model.Site.SiteName)</dd><dt>
            @Html.DisplayNameFor(model => model.Site.SiteTypeID)</dt><dd>
            @Html.DisplayFor(model => model.Site.SiteType.SType)</dd><dt>
            @Html.DisplayNameFor(model => model.Site.SiteAddress)</dt><dd>
            @Html.DisplayFor(model => model.Site.SiteAddress)</dd><dt>
            @Html.DisplayNameFor(model => model.Site.SiteSuburb)</dt><dd>
            @Html.DisplayFor(model => model.Site.SiteSuburb)</dd><dt>
            @Html.DisplayNameFor(model => model.Site.SitePostCode)</dt><dd>
            @Html.DisplayFor(model => model.Site.SitePostCode)</dd><dd><table class="table"><tr><th>
                        Booking Date</th><th>
                        Job Description</th><th>
                        Invoice Date</th></tr>




                @foreach (var item in Model.Site.Job.Take(9))
                {
                    <tr><td>

                            @Html.DisplayFor(modelItem => item.BookingDate)</td><td>
                            @item.JobDescription.Substring(0, Math.Min(item.JobDescription.Length, 38))</td><td>
                            @Html.DisplayFor(modelItem => item.InvoiceDate)</td><td><a asp-controller="Jobs"
                               asp-action="Details"
                               asp-route-id="@item.JobID">More Details</a></td></tr>
                }</table></dd><div><a asp-action="Edit" asp-route-id="@Model.SiteID">Edit</a> |<a asp-action="Index">Back to List</a></div>

This works fine if there is one job for the site in question.

However when there is more than one job for the site, which happens a lot, the view is loading this error.  Why, what do I need to change?:

A database operation failed while processing the request.

InvalidOperationException: Sequence contains more than one element

There are pending model changes for EVAContext

In Visual Studio, use the Package Manager Console to scaffold a new migration for these changes and apply them to the database:
PM> Add-Migration [migration name]
PM> Update-Database

Alternatively, you can scaffold a new migration and apply it from a command prompt at your project directory:
> dotnet ef migrations add [migration name]> dotnet ef database update 


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>