Fixing error “No Entity Framework provider found for the ADO.NET provider with invariant name ‘System.Data.SqlClient'”

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

This post describes the fix to error “No Entity Framework provider found for the ADO.NET provider with invariant name ‘System.Data.SqlClient'”, which Visual Studio throws at your face when you try to run an application on any Windows-based system (or which you’ve dug out of event logs). Also, you’re probably using Entity Framework in your project.

Error

When debugging/running your code you get an error like this:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

The running of the program is stopped there, and removing and readding the nuget packages and/or other references to dlls does not help. I, at least, tried also making all kinds of changes to my web.config and ran iisreset a couple of times, but nothing seemed to help. There’s a simple fix available, though!

Continue reading

Applying Entity Framework’s Code-First Migrations against a Database in Azure by running Update-Database

This post describes how to run Entity Framework’s code-first migrations against a database located in the Windows Azure. This is done by running Update-Database commandlet with suitable switches, see below.

The problem and symptoms

Okay, so you’re developing your MVC+EF cool web app with a database in Azure, and you’re using code-first migrations. Cool! What’s nice with code-first-migrations is the fact they are run automatically even in the cloud the next time your app is running (as long as you publish your app with that little box ticked – something like in the screen capture below). But wait – what if there are conflicts – what kind of errors are you going to get?

 

Azure Web Publish

Azure Web Publish

Not very useful ones, I’m afraid, and it’s a pain navigating the Azure portal to fetch the log files. At some point – for me, it wasn’t the first time I ran the web app, but the phase when I was logging in – you’ll be getting the error the migrator internally throws. That might be enough to point you to the right direction, and maybe you’ll be able to figure out what’s wrong! But if that’s not the case, here’s the way to run Update-Database against your Azure Database!

Continue reading

Exception of type ‘System.OutOfMemoryException’ was thrown.

IIS8

This post describes one of the more no-brainerish ways of fixing a ‘System.OutOfMemoryException’ exceptions being thrown in your MVC ASP.Net application using Entity Framework.

Problem

While developing a web project, for example ASP.NET MVC with EF, sometimes when handling a lot of data or complex entities on your dev machine, you encounter this error:

OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
   System.Text.StringBuilder.ToString() +35
   System.IO.StreamReader.ReadToEnd() +123
   System.Web.Optimization.BundleFile.ApplyTransforms() +74
   System.Web.Optimization.DefaultBundleBuilder.BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable`1 files) +472
   System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext context) +127
   System.Web.Optimization.Bundle.GetBundleResponse(BundleContext context) +45
   System.Web.Optimization.BundleResolver.GetBundleContents(String virtualPath) +166
   System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1 assets) +205
   System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat, String[] paths) +35
   System.Web.Optimization.Scripts.RenderFormat(String tagFormat, String[] paths) +107
   System.Web.Optimization.Scripts.Render(String[] paths) +21

Reason

By default, Visual Studio uses 32-bit version of IIS Express for your deployments. In case you know what you’re doing, it’s safe and easy to change this, though.

Continue reading

Fixing the error: “Column XX in table dbo.YY is of a type that is invalid for use as a key column in an index.”

entity-framework-logo

While using Entity Framework and code-first migrations, Entity Framework (EF) creates the indexes for you – but what if you need to create a custom one, explicitly based on certain field / column? Then you’ll have to tell EF which one to use as a key column. Usually, it’s easy – you just add the following annotation to the columns you’ll be using:

[Index("OfficialIds", 1)]
public int AreaId { get; set; }

[Index("OfficialIds", 2)] 
public string EstateId { get; set; }

(example stripped of extra code and other columns for clarity)

And after adding the migration (Add-Migration… commandlet) you get something like this:

CreateIndex("dbo.Areas", new[] { "AreaId", "EstateId" }, name: "OfficialIds");

Okay, nice. Don’t have to create the indexes myself either, so that’s neat with EF.

But what if, when running Update-Database, you get an error like:

Column XX in table dbo.YY is of a type that is invalid for use as a key column in an index.

There’s a quick and simple solution.

Solution: limit the length of your VARCHARs for something suitable for an actual key column!

This exception comes from the fact that indexes in EF have the maximum length of 900 chars. If you are like me, you’ll have been a little lazy and you have created your string-typed model variables without specifying a maximum length for them, and this causes the length of the index values to be way too long.

Continue reading

Fixing “An error occurred while updating the entries” while running code-first migrations in MVC 5 app

Update-Database error

This post describes an issue with EF’s code-first migrations, when mapping between DB’s DateTime and C#’s DateTime kind of fails, and results in Update-Database cmdlet failing. It’s more or less a prime example of a situation, where the error itself tells very little about the actual issue, and since debugging code-first migrations is kind of difficult (see the best tips for that here!), it’s cumbersome to investigate.

Symptoms

While running Update-Database in a code-first ASP.NET MVC5 + EF6 -project, you get a following (or similar) error:

An error occurred while updating the entries. See the inner exception for details.

The whole, pretty terrifying error message is the following:

Continue reading