0

I am currently working on a ASP.NET MVC project for which I created a controller with an Action method which will return a JsonResult. The action basically goes into a DB and based on a table name passed to the action should do a dynamic linq query and return the rows for the desired table.

My Action method looks like this:

public async Task<JsonResult> FieldValues(string table)
        {
            using (DbEntities ctx = new DbEntities())
            {                
                Type type = System.Reflection.Assembly.GetExecutingAssembly().GetType("MyWebApp.Models." + table);
                var query = ctx.Set(type).SqlQuery(string.Format("SELECT * FROM {0}", table));                               
                var data = await query.ToListAsync();               
                return Json(data, JsonRequestBehavior.AllowGet);
            }
        }

This action is invoked using jquery ($.get(...)) from the view... I tried to debug the action, the table is passed properly, the query seems ok, but the return statement is never reached!

It's the first time I am using an async action method and also the first time I'm using "dynamic linq".

Any help on what I might have done wrong is appreciated!

Thank you!

AndreiC
  • 1,490
  • 4
  • 16
  • 34
  • Anything being returned to your caller (the jQuery `$.get` call)? Does this succeed if you make this a synchronous call (remove `async` and call `ToList` instead of `ToListAsync`)? – David Hoerster May 16 '14 at 14:16
  • no, the success method in my $.get call is not reached as the action method in the controller doesn't return. It's weird, but I can't call ToList on the query object - only ToListAsync is available... – AndreiC May 16 '14 at 14:33
  • How big are these tables? What if you temporarily modify your query to "SELECT TOP 1 * FROM {0}"? – Sven Grosen May 16 '14 at 14:39
  • Does your Controller derive from `AsyncController`? –  May 16 '14 at 14:49
  • after a bit more debugging (decided to use the done and fail methods available for the jquery promise returned by $.get) I saw that the call fails with: the message "The data reader is incompatible with the specified 'DataModel.AR15'. A member of the type, 'AR151', does not have a corresponding column in the data reader with the same name." - now I just need to understand this message and fix it :) – AndreiC May 16 '14 at 14:49
  • Maybe this helps: http://stackoverflow.com/questions/10635277/the-datareader-is-incompatible OR THIS http://stackoverflow.com/questions/14235783/the-data-reader-is-incompatible-a-member-does-not-have-a-corresponding-column – David Hoerster May 16 '14 at 14:53

1 Answers1

0

ok... found the issue! Long story cut short: the DB tables that I work with have a column which has the same name as the table itself. EF got "scared" and renamed the column in the generated class and added a 1 at the end (class name AR15, and property created was AR151 instead of AR15). Due to this, the query was failing silently (no exception) - I just saw it when I added the done and fail methods for the $.get promise. The solution was to create manually a new entity in the model with the structure I needed and which was common for all the tables. Once that was done the code worked like a charm and I even remove the reflection as I knew the entity type already (the newly created one).

Thanks for the help and suggestions!

AndreiC
  • 1,490
  • 4
  • 16
  • 34