[back compat] Designer: Error opening EF5 edmx with decimal precision in EF6 designer


Issue reported via Connect - http://connect.microsoft.com/VisualStudio/feedback/details/800178/cannot-load-mymodel-edmx-specified-cast-is-not-valid-when-clicking-edmx-file-in-project

EF is using a singleton object of the internal Unbounded type for unbounded facet values. To check if a facet is unbounded you just do a reference comparison to the singleton instance. The LegacyProviderManifestWrapper translates EF5 metadata types to EF6 metadata types and vice versa. When doing the conversion we used the wrong (e.g. the one defined in EF6 (EntityFramework.dll) rather than the one defined in EF5 (System.Data.Entity.dll)) reference for checking if the facet value is unbounded. This resulted in not recognizing Unbounded value and led to casting the instance of Unbounded type to byte which caused the InvalidCastException.

This was already reported twice after VS2012 RC was shipped. In one case it is impossible to open an edmx file. In the other case it is impossible to import functions - in both cases the culprit seems to be a decimal parameter in a stored proc.
Closed Apr 24, 2014 at 1:48 AM by lukew
Verified Fixed


Mugdhak wrote Sep 16, 2013 at 5:39 PM

** Closed by Mugdhak 09/16/2013 9:39AM

ahmadm wrote Apr 14, 2014 at 1:21 PM

Has this issues resurfaced with EF6.1 Tooling for VS2012? I seem to be having similar issues with decimal parameter (http://www.microsoft.com/en-us/download/details.aspx?id=40762)

moozzyk wrote Apr 14, 2014 at 5:51 PM

@ahmadm - I am not aware of any regression in this area. If you see this please create a new bug and provide a repro. Without a repro it is hard to tell what's going on.

Falagard wrote Apr 15, 2014 at 8:00 PM

I'm having the same problem. I finally tracked down these posts about it.

I'm not sure the steps I took, other than attempting to add a stored procedure with decimal parameters to my edmx model and it giving me an "invalid cast" exception, and making my edmx unloadable from that point on.

I attempted to upgrade to EF6.1 and also downloaded the EF6.1 tools but am still getting the error.

Now I'm working on downgrading everything, but thought I'd post to say I hit the same problem.

moozzyk wrote Apr 15, 2014 at 9:39 PM

@Falagard - can you show your stored proc? I am not interested in the actual logic but rather the signature and what it returns.

moozzyk wrote Apr 16, 2014 at 5:08 AM

Note for triage: Looks like the fix we made for EF 6.0 tooling might not have been ported when we moved the code to the OSS branch.

Falagard wrote Apr 16, 2014 at 9:53 AM

ALTER PROCEDURE [dbo].[SelectUnitsByDistance] 
    @Longitude decimal(10,6),
    @Latitude decimal(10,6),
    @Parking bit,
    @DesiredDistance int
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

    FROM Unit u
    JOIN Building b ON u.BuildingID = b.BuildingId
    JOIN [Address] a ON a.AddressID = b.AddressID
    geography::Point(a.Latitude, a.Longitude, 4326).STDistance(geography::Point(@Latitude, @Longitude, 4326)) / 100 < @DesiredDistance

moozzyk wrote Apr 16, 2014 at 5:58 PM

@Falagard - Thank you. I confirmed today that the fix we made for EF6 tooling was lost when moving code to codeplex and hence reactivated this bug.

Falagard wrote Apr 17, 2014 at 3:27 PM

Thanks for confirming. Any idea when this might be fixed?

moozzyk wrote Apr 17, 2014 at 4:30 PM

ahmadm wrote Apr 22, 2014 at 5:29 PM

@moozzyk, @Falagard - after much effort (which included setting up a new laptop with VS2012 and EF6.1), I added an entry to my legacy EF5 EDMX. In the section
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
I added the following which seemed to have resolved my issue. I am now able to open an older EF5 EDMX within the EF6.1 designer tooling. This somehow does not make sense to considering the comment made in https://entityframework.codeplex.com/discussions/470300
<DesignerProperty Name="UseLegacyProvider" Value="false" />

moozzyk wrote Apr 22, 2014 at 6:10 PM

@ahmadm - when you do that the designer internally use the real EF6 provider to do the work. When the setting is not there or the Value is true the designer uses a wrapping provider which exposes EF5 provider as an EF6 provider and the bug is in this wrapping provider. One issue might have is that when you update the model from the database and you are using Sql Server 2012 the SSDL will contain "2012" as the provider manifest token value which was not supported in EF5. Which will make your app fail at runtime.

divega wrote Apr 25, 2014 at 11:08 PM

Opening an existing EF 5 Designer model (.EDMX file) using Entity Framework 6.0.2 o 6.1.0 Tools in Visual Studio 2012 or Visual Studio 20123 may fail with a message saying “Cannot load ‘filename.edmx’: Specified cast is not valid”. This only occurs if the model in question contains function imports with parameters of type ‘decimal’.

If you have installed those versions of Entity Framework Tools for Visual Studio 2012 or Visual Studio 2013 from the Microsoft Download Center or if you have either installed Visual Studio 2013 Update 1 or Update 2, the version of the designer you are using has this issue. The original Entity Framework 6.0.0 originally included in Visual Studio 2013 and available in the Download Center are not affected.

If your EF model does not contain function imports for stored procedures that return objects with decimal properties then you won't hit this issue.

The cause of the issue is a bug in the code that interprets the EF 5 metadata about the function parameter that causes the Designer to wrongly try to cast a value to byte if the parameter has no precision and scale facets.

While we prepare to release an updated version that contains the fix, you can use the following workarounds:
  1. Revert your setup to the 6.0.0 version of the Entity Framework Tools: To do this you need to manually uninstall any more recent version of the tooling using the "Add or remove programs" window and reinstall the 6.0.0 version. For Visual Studio 2012 you can find it in the Microsoft Download Center (TODO: Rowan has the link). For Visual Studio 2013 the EFTools.MSI and EFTools.cab files were originally included in the Visual Studio setup, so you might able to revert to the 6.0.0 version of the tools by uninstalling them and then repairing Visual Studio, or by finding the MSI in the Visual Studio setup media.
  2. Modify the EDMX files in an editor: An alternative workaround requires manual modification of the EDMX files using either a text or an XML editor. Be careful to create backup copies of your original EDMX files and not to make any additional changes on them that could cause them to become invalid.
The modification need to be applied to <parameter> elements of any <function> (e.g. stored procedures or Table-Valued Functions) inside the <edmx:StorageModels> section (also known as the ‘SSDL’ section) of the EDMX for all parameters that map to decimal parameters in the corresponding function import in the ‘CSDL’ section. For examples, for the following function:
<Function Name="Product_Insert" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
      <Parameter Name="Id" Type="int" Mode="In" />
      <Parameter Name="Name" Type="int" Mode="In" />
      <Parameter Name="Price" Type="numeric" Mode="In" />
The ‘Price’ parameter has to be changed as follows:
    <Parameter Name="Price" Type="numeric" Mode="In" Precision="8" Scale="4" />
Note that the actual numeric values assigned to Precision and/or Scale are not important in this case.

divega wrote Apr 25, 2014 at 11:51 PM

Here is a direct link to the EF 6.0.0 Tools download should you pick that as a workaround:


doogie04 wrote May 10, 2014 at 6:13 PM

Just an FYI, but I have this issue, but none of the functions I'm importing use decimal. Types they use: Uniqueidentifier, int, varchar, nvarchar, datetime, tinyint, and bit.

Downgrading the tool resolved the problem.

Steps for VS 2013 downgrade, since there is no file to download:
1) In control panel, find "Entity Framework 6.x.x Tools for Visual Studio 2013" and uninstall
2) Find your install media (ISO in my case), find the \packages\EFTools folder
3) Reinstall EF tools from the folder in step 2.

RatStrydom wrote May 19, 2014 at 6:38 AM

Still a problem in tools 6.1. If you fix the model file and refresh the db it saves it without the precision and scale again and the file is broken again.

hubble662 wrote May 19, 2014 at 2:27 PM

Hoping to solve the problem I uninstalled EF6.1 from control panel, but now I can't find a way of re-installing it, I can't find EFTools.msi on my hd, and trying to repair VS2013 installation didn't work.
I can't find EF6 stand alone installation anywhere (I am talking about VS2013)...
What else can I try?

Thanks for help.

lukew wrote May 19, 2014 at 11:01 PM

You can get the msi for EFtools from the download center, there are MSI files for both 2012 and 2013: http://www.microsoft.com/en-us/download/details.aspx?id=40762

sjnaughton wrote May 20, 2014 at 10:16 PM

When will there be a version of the EF Tools 6.1?? we can install that fixes this?

divega wrote May 21, 2014 at 2:36 PM

@sjnaughton: The fix is included in the tools for EF 6.1.1 beta released yesterday:


Hope this helps.

sjnaughton wrote May 26, 2014 at 11:57 AM

Well that fixes it for me in VS2013 and VS2012 thanks muchly :)

JendaPerl wrote Jul 30, 2014 at 7:17 PM

Thank you very much for wasting about three hours of my time. If the message gave a little more details I would know it's not somewhere inside my VSIX.
Anyway adding those Precision and Scale attributes to all then fifty Type="numeric" and Type="decimal" missing them helped.
And by the way ... updating to EF 6.1.1 did not help.