Search code examples
ef-code-first

System.InvalidOperationException: The store type 'decimal(18,4)' could not be found in the SqlServer provider manifest


I am constructing my connection string the following way (with appropriate values for connection parameters).

var builder = new SqlConnectionStringBuilder
{
    PersistSecurityInfo = false,
    InitialCatalog = "mydatabase",
    UserID = "myuser",
    Password = "mydatabase",
    DataSource = "myserver"
};
var connectionString = builder.ConnectionString;
using (var db = Helper.MakeContext(connectionString)) {
    var carrier = db.Carriers.FirstOrDefault(); // fails here with error
    Console.WriteLine(carrier.Carrier);
}

Helper is in a different project

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using ApiForVivtrack3.Entities;
    
public static class Helper
{
    //public  string ConnectionString { get; set; }
    public static ApiDbContext MakeContext(string connectionString)
    {
        var db = new ApiDbContext(connectionString);
        return db;
    }
}

The EF 6.2 DbContext is set up as follows

using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;

namespace ApiForVivtrack3.Entities
{
    public partial class ApiDbContext : DbContext
    {
        public ApiDbContext(string efConnectString)
            : base(efConnectString)
        {

        }

        // DbSet declarations
    }
}

The call stack is

Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception: 
System.InvalidOperationException: The store type 'decimal(18,4)' could not be found in the SqlServer provider manifest
    at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty column, EntityType table, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty column, EntityType table, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.<>c__DisplayClass4.<Configure>b__3(Tuple`2 pm)
   at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, ICollection`1 entitySets, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at UnitTestProject1.UnitTest1.TestMethod1() in D:\devnet10\SBD.Common\UnitTestProject2\UnitTest1.cs:line 39

I have done a text search for Decimal(18, but nothing comes up.


Solution

  • I understand this is an OQ. Seeing as it is unanswered, I'll share what I had to do.

    Do a Find (ctrl F) for (18, 4) or what ever the values are in the error message. Most likely you will find the text strings show up in the Fluent API section of your solution. In my case, I copied Fluent API code from a CORE application into my EF 6 application. Core used "HasColumnType()" which is not compatible with EF 6 that uses "HasPrecision()".

    Replace "HasColumnType(" with "HasPrecision(" and run the code again...