System.InvalidOperationException: 在 SqlServer 提供程序清单中找不到存储类型 'decimal(18,4)'

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

我正在按照以下方式构建我的连接字符串(使用适当的连接参数值)。

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);
}

助手在不同的项目中

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;
    }
}

EF 6.2 DbContext设置如下

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
    }
}

调用堆栈为

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

我已经对 Decimal(18 进行了文本搜索,但没有任何结果。

我知道这是 OQ。鉴于未得到解答,我将分享我必须做的事情。

查找 (ctrl F) (18, 4) 或错误消息中的任何值。您很可能会发现文本字符串显示在解决方案的 Fluent API 部分中。 就我而言,我将 Fluent API 代码从 CORE 应用程序复制到我的 EF 6 应用程序中。使用的核心 "HasColumnType()" 与使用 "HasPrecision()".

的 EF 6 不兼容

将 "HasColumnType(" 替换为 "HasPrecision(" 并再次 运行 代码...

正如另一个答案所指出的,如果您尝试使用取自 Entity Framework 核心配置的语法来配置小数列,则 Entity Framework 6 可能会抛出此错误。 HasColumnType("decimal(18,4)") 需要替换为 HasPrecision(18,4)

这是一些正则表达式,可用于替换代码中的所有实例 (regexr demo)。替换为:

\.HasColumnType\("decimal\((\d+),(\d+)\)"\)

有了这个:

.HasPrecision(, )

我已经在 J​​etBrains Rider 中测试了这个正则表达式,但请注意其他 IDE 可能使用不同的正则表达式风格。希望这能让您入门,即使您需要修改它。