G_Man
G_Man

Reputation: 1343

Entity Framework and Table Relationship Error Out

I am new to Entity Framework and having a hard time figuring out what I am doing wrong. Using ASP.Net MVC, C# and EF6 on a normal SQL database.

Here is my controller code.

using (var context = new MyEntities())
        {
            var result = context.Streamers.OrderBy(x => x.AddedDate);
            foreach (var item in result)
            {
                var linkResult = context.Links.Where(x => x.StreamerId == item.Id);
                item.Links = linkResult.ToList();
            }
            return View(result.ToList());
        }

Here is my View

@{int i = 0;}
@foreach (var streamer in Model)
{
var className = i % 2 == 0 ? "col-lg-6 order-lg-2" : "col-lg-6";
var className2 = i % 2 == 0 ? "col-lg-6 order-lg-1" : "col-lg-6";
i++;
<section>
    <div class="container">
        <div class="row align-items-center">
            <div class="@className">
                <div class="p-5">
                    <a href="@streamer.StreamerUrl" target="_blank">
                        <img class="img-fluid rounded-circle" src="@streamer.StreamerImage" alt="@streamer.StreamerName">
                    </a>
                </div>
            </div>
            <div class="@className2">
                <div class="p-5">
                    <a href="@streamer.StreamerUrl" target="_blank">
                        <h2 class="display-4">@streamer.StreamerName</h2>
                    </a>
                    <p>@streamer.StreamerDescription</p>
                    <ul>
                        @foreach (var link in streamer.Links)
                        {
                            <li><a href="@link.LinkAddress" target="_blank">@link.LinkName</a></li>
                        }
                    </ul>
                </div>
            </div>
        </div>
    </div>
</section>
}

The code is erroring out on the for each for streamer.Links

System.ObjectDisposedException: 'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'

Error

Can anyone help me figure out what I am doing wrong here? Is there a much simpler way to be doing this? Thanks in advance

Upvotes: 0

Views: 36

Answers (3)

Harikrishna
Harikrishna

Reputation: 63

Try this if you don't want to change Html:

using (var context = new MyEntities())
  {
           result = context.Streamers.Include(c => c.Links)
           .OrderBy(c=> c.AddedDate).ToList();
   }

Upvotes: 2

Serge
Serge

Reputation: 43870

Try to use this code:

List<Streamer> result;
using (var context = new MyEntities())
  {
           result = context.Streamers.OrderBy(x => x.AddedDate).ToList();
         foreach (var item in result)
         {
  item.Links =  = context.Links.Where(x => x.StreamerId == item.Id).ToList();
         }
  }
  return View(result);

but since you are not filtering streamers you can try this:

using (var context = new MyEntities())
  {
           result = context.Streamers.Include("Links").OrderBy(x => x.AddedDate).ToList();
       
  }

Upvotes: 1

Maxim Zabolotskikh
Maxim Zabolotskikh

Reputation: 3367

Your DbContext is likely disposed. When for loop hits, Link is not attached to the context and cannot load the navigational property with the address. Load your link addresses when querying the links:

context.Links.Include(c => c.LinkAddress).Where(x => x.StreamerId == item.Id);

Upvotes: 2

Related Questions