One to Many Relationship causing issues...

One to Many Relationship causing issues...

HillChris1234HillChris1234 Posts: 27Questions: 13Answers: 2
edited May 2016 in Free community support

I have a DataTable that I've implemented in my MVC Application. The table displays a list of Employers, and each Employer has an associated "ApplicantTypeOption" in a one-to-many relationship. That relationship is defined in the SQL database. I want to use the "serverSide" option, so the data is being organized in the controller and returned to the view via Json. When I run my page, I get this error: "DataTables warning: table id=tblBrokers - Ajax error. For more information about this error, please see http://datatables.net/tn/7"

When I debug in Chrome, I see a generic 500 error ("There is a problem with the resource you are looking for, and it cannot be displayed."). What I've done to this point is create a new Broker class that inherits the Employer class. I generate the Employer data set, then loop through it and assign the elements to an instance of this new Broker class. Like so:

var v = (from a in context.Employers where a.Type == CompanyType.Broker select a);
v = v.Skip(skip).Take(pageSize);
List<Broker> Brokers = new List<Broker>();

foreach (Employer e in v.ToList())
{
    Brokers.Add(new Broker {
        Id = e.Id,
        Name = e.Name,
        Code = e.Code,
        NavigationTileMatrixID = (e.NavigationTileMatrix != null ? (int?)e.NavigationTileMatrix.Id : null)
    });
}

var data = Brokers.ToList();
return Json(new { draw = draw, recordsFiltered = totalRecords, recordsTotal = totalRecords, data = data }, JsonRequestBehavior.AllowGet);

So, I'm returning the Brokers list rather than the Employers. This won't do, because I need to utilize some of the entities (like the ApplicantTypeOption) on the grid. Even when I assign those to the Broker class, I get this error. The error has to be related to this 1:Many relationship setup. I can't very will put ALL my code here, but here's how these relationships are set up in the Employer class:

[Display(Name = "Applicant Type Defaults", ShortName = "App Types")]
public virtual ICollection<ApplicantTypeOption> ApplicantTypeOptions { get; set; }

Does anyone know why I can't just use the Employer class as it is to return Json data back to the DataTable?

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • HillChris1234HillChris1234 Posts: 27Questions: 13Answers: 2
    edited May 2016

    I just checked my Event viewer, and this is what I got:
    The operation cannot be completed because the DbContext has been disposed.

    Not sure what EXACTLY that means... I have a general idea, but I don't know what to do aboot it.

  • allanallan Posts: 63,872Questions: 1Answers: 10,528 Site admin
    Answer ✓

    Thanks for checking the event viewer, I was going to suggest setting VS to break on error, but it sounds like you've done that.

    I don't actually see any database interaction code in the above - I guess that's hidden in your classes. That probably makes this more of a general C# question that one specifically about Editor. What is the code around the DbContext error. It basically means that an old database transaction hasn't been cleaned up, or an old one is being reused.

    Normally I would suggest using the database connection in a using statement so C# will tidy it up for you. You can see an example of that being used with the Editor libraries in the code here.

    Allan

This discussion has been closed.