I’m always somewhat amazed when I read something from documentation that is not straight forward and it actually works.  So amazed and excited that I often feel the need to blog about it.

So, here is the problem.  I created a CodeFirst implementation of EntityFramework in Visual Studio and created a simple hierarchy in my data model.  That is, without showing all the code, here is what I have:

public class Person{    [Key]    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]    public int Id { get; set; }

    public virtual List<EmailAddressInfo> EmailAddressInfoList { get; set; }    ..}

public class EmailAddressInfo{    [Key]    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]    public int Id { get; set; }

    public string EmailAddress { get; set; }

}

public class EmailAccount{    [Key]    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]    public int Id { get; set; }    ...}

Now, if you execute a LINQ command like the following:

var recs = (from data in db.Persons                            select data);                foreach (var rec in recs)                {                    foreach (var email in rec.EmailAddressInfoList)                    {                        Console.WriteLine(email);                    }                }

You get the error:

Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

 

image

 

To solve this, you need to tell EntityFramework/CodeFirst to include the EmailAddressInfoList in the query.  All you need to do is change the db.Persons to db.Persons.Include("EmailAddressInfoList") as follows:

 

var recs = (from data in db.Persons.Include("EmailAddressInfoList")            select data);

 

Then, it all works!

Hope this helps.

Normally, when we stick a JSON file up on an IIS web server, all we have to do to get to is is to set the Mime type.  One easy way to do it is to add to your web.config the following:

 

<system.webServer>    <validation validateIntegratedModeConfiguration="false" />    <modules runAllManagedModulesForAllRequests="true" />    <staticContent>      <mimeMap fileExtension=".json" mimeType="application/json"  />    </staticContent>    ...

 

This works great as long as the GET verb is used (or just enter the on the url like http://mysite.com/myfile.json).

So, what if you "need” to use the POST keyword.  Say for example, you can not change the JavaScript file to use GET instead of POST to get the file.  I was hoping to find some simple web.config parameter to set but I had no luck.  I even posted to StackOverflow and so far, the answers have been less than helpful.  Who knows, maybe by the time you read this, there will be a better answer then my solution which is to write a simple asp.net handler and register it to type json.

 

So, here is the simpler handler that does the trick:

 

public class JSONHandler : IHttpHandler    {

        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "application/json";            string output = System.IO.File.ReadAllText(context.Request.PhysicalPath);            context.Response.Write(output);

        }

        public bool IsReusable        {            get            {                return false;            }        }    }

 

and the associated web.config entries

 

<httpHandlers>     <add verb="*" path="*.json" validate="false" type="JSONHandler" />   </httpHandlers> </system.web>

 <system.webServer>   <validation validateIntegratedModeConfiguration="false" />   <modules runAllManagedModulesForAllRequests="true" />   <staticContent>     <mimeMap fileExtension=".json" mimeType="application/json"  />   </staticContent>   <handlers>     <add verb="*" path="*.json" name="JSONHandler" type="JSONHandler"/>   </handlers> </system.webServer>

I’m assuming there is a simple way, but for now, this works for me.  Please post a better solution and reference it or just tell me in the comments.


© 2012 PeterKellner.net. All Rights Reserved
Follow

Get every new post delivered to your Inbox

Join other followers: