Nov 11

Using code first approach with Entity Framework generates Unable to load the specified metadata resource error

I’ve setup a solution with a WebAPI and a MVC 4 application. I’m using the Entity Framework 5 which stores it data into a SQL Azure database. In the package manager console I typ: enable-migrations and get the error:

Unable to load the specified metadata resource.

This due to the context connectionstring in the web.config. Check if you have the right settings under <connectionStrings>

 

May 06

Error making asyn call in Entity Framework: There is already an open DataReader associated with this Command which must be closed first

Using Entity Framework 6.0 I got this message. This problem occurs when mostly in Async environment. This is due to a reader (in the same DBContext) that is still open, while another reader tries to open an new one. Typically this is a async issue.

In my MVC 5 app I have a Async call to my webapi. My problem was that I created a second repository object in a method, while the first repository was created by Unity Dependency Injection Framework was not finished reading.

My solution:

In stead of newing the repository up in the method I injected it in the constructor and all issues where solved.

A better solution:

Add MultipleActiveResultSets=True to the connectionstring of the database.

Dec 24

Typescript Common errors and issues for newbies

Recently I started working with Typescript. I’ve noticed that I made some Typescript Common errors and issues for newbies. So I’ve deceided to blog about them. I hope others can benefit from this:

Error 1:
Javascript critical error at line X, column Y, in http…./yourfile.ts
SCRIPT1004: Expected ‘;’

Solution 1:
You’ve probably included the .ts (TypeScript) file in your HTML. But you must include the generated .js (JavaScript) file. Replace the .TS extention for .JS extention, compile the project and you’re all set.

Error 2:
Unhandled exception at line X, column Y in http://localhost/…..yourfile.js.

0x800a138f – JavaScript runtime error: Unable to get property ‘YOURCLASS’ of undefined or null reference

Solution 2:
You’ve probably NOT include the .js file in your HTML file. Include the file <script scr=”YOURFILE.js”></script> in the HTML page.

Error 3: (this error occurs at Typescript compile time, in the editor, at compile time)
Invalid ‘new’ expression

Solution 3:
You must include the correct namespace for the class to create:

Wrong: new Project();
OK: new MySolution.Classes.Project()

Error 4: (this error occurs in the editor, at compile time)
The property XX does not exists on value of type YY

Example code:
public Project: Myapp.Models.ProjectInfo;

Solution 4:
Although you’ve probably used Intellisense when editing. Still this error does appear. You must include a reference to the .ts file. Put the following line at top of the Typescript file that generates this error:

/// <reference path=”PATH TO YOUR TS CLASS FILE”/>

Error 5:
Supplied parameters do not match any signature of call target: type ‘XYX’ is missing property ‘concat’ from type ‘XYX[]’

Solution 5
The parameter expected is an array of XYZ, but the passed parameter is XYZ. Replace the parameter with the correct one (should be an array in this case).

 

Hope these error messages and the solutions I have provided you, will give you a head start. In my case, most error gave me a terrible a headache.

Happy coding

Mar 22

Solving error 500 issues using the Enitity Framework with MVC 4 Web api

I have a MVC 4 applications that calls a Web Api REST service. The webservice retrieves the values from a Windows Azure database using the Entity Framewok. For some reason a call to get a user from the web api fails. After inspecting the respons with the Fiddler2, I saw this error:

 

HTTP/1.1 500 Internal Server Error

ExceptionMessage=The ‘ObjectContent`1′ type failed to serialize the response body for content type ‘application/json; charset=utf-8′.

ExceptionMessage=Error getting value from ‘Tenant’ on ‘System.Data.Entity.DynamicProxies.User_A66506F1AD16B9BC58E2CE2527D9CEDD7F1AFFBF3191ECC41B3556′.

 

The strange thing is, that sometimes the call is made correctly. I’m running the application locally on my development workstation and especially when I step through the code, the call is miraculously completed correctly.

After hours of search I read in Professional C# 2012 and .NET 4.5that there are different setting in the way the object loads from the database using the Entity Framework. The setting from your datacontext must be set to LazyLoadingEnable = false. So my code looks as follows:

var db = new BsoDataModelContainer();
db.Configuration.LazyLoadingEnabled = false;

Quite simple, once you know IT.

.