简体   繁体   English

在 SqlServer 提供程序清单中找不到存储类型“Varchar(100)”

[英]The store type 'Varchar(100)' could not be found in the SqlServer provider manifest

I am working on an MVC 4 project that use EF 6 for all operations to and from the Database (SQL Server).我正在开发一个 MVC 4 项目,该项目使用 EF 6 进行所有进出数据库(SQL Server)的操作。 I had to do some refactoring to use a more explicit Fluent API.我必须进行一些重构才能使用更明确的 Fluent API。 Basically, now every entity on the project has its corresponding Fluent API code for all mappings.基本上,现在项目中的每个实体都有对应于所有映射的 Fluent API 代码。

The problem comes when I try to run the EF commands from Package Manager Console.当我尝试从包管理器控制台运行 EF 命令时,问题就出现了。 This is the error I am getting: The store type 'Varchar(100)' could not be found in the SqlServer provider manifest这是我得到的错误:在 SqlServer 提供程序清单中找不到存储类型“Varchar(100)”

This is the complete log of the error:这是错误的完整日志:

PM> enable-migrations
Checking if the context targets an existing database...
System.InvalidOperationException: The store type 'Varchar(100)' 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.<>c__DisplayClass47_0.<Configure>b__0(Tuple`2 pm)
   at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
   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.LazyInternalContext.get_ModelBeingInitialized()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Infrastructure.Design.Executor.CreateMigrationScaffolder(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Infrastructure.Design.Executor.ScaffoldInitialCreateInternal(DbConnectionInfo connectionInfo, String contextTypeName, String contextAssemblyName, String migrationsNamespace, Boolean auto, String migrationsDir)
   at System.Data.Entity.Infrastructure.Design.Executor.ScaffoldInitialCreate.<>c__DisplayClass0_0.<.ctor>b__0()
   at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
   at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.Execute(Action action)

I have been reading a lot of blogs/ post how to solve this but none of them seems to work.我一直在阅读很多博客/帖子如何解决这个问题,但它们似乎都不起作用。 These are a few things I have tried:这些是我尝试过的一些事情:

  1. Reinstalling EntityFramework.dll and EntityFrameWork.SqlServer.dll重新安装 EntityFramework.dll 和 EntityFrameWork.SqlServer.dll
  2. Adding a dummy method to explicitly load the assembly.添加一个虚拟方法来显式加载程序集。 This is the link这是链接
  3. Originally I was using EF 6.3.0 and upgraded to EF 6.4.0.最初我使用的是 EF 6.3.0 并升级到 EF 6.4.0。 I thought the upgrade was the problem so I downgraded but still having the same issue.我认为升级是问题,所以我降级但仍然遇到同样的问题。

Updated This is the code for the Entity:更新这是实体的代码:

public class EntityA: BaseEntity
    {
        /// <summary>
        /// Gets or sets Contact identifier
        /// </summary>
        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public bool IsActive { get; set; }
}

Using Fluent API configuration in a separated class:在单独的类中使用 Fluent API 配置:

public class EntityAConfiguration : EntityTypeConfiguration<EntityA>
    {
        public EntityAConfiguration()
        {
            this.ToTable("EntityA")
                .HasKey(c => c.Id);

            this.Property(c => c.Id)
                .HasColumnName("Id")
                .HasColumnType("int")
                .IsRequired();

            this.Property(c => c.Name)
                .HasColumnName("Name")
                .HasColumnType("Varchar(40)")
                .IsRequired();

            this.Property(c => c.Description)
                .HasColumnName("Description")
                .HasColumnType("Varchar(100)")
                .IsRequired();

            this.Property(c => c.IsActive)
                .HasColumnName("IsActive")
                .HasColumnType("bit");
        }
    }

BaseEntity class is only having two properties: CreatedDate, UpdatedDate. BaseEntity类只有两个属性:CreatedDate、UpdatedDate。 I use that class in all Entities that needs to store that info.我在需要存储该信息的所有实体中使用该类。

At this point I am running out of ideas how to solve this.在这一点上,我已经没有如何解决这个问题的想法了。 If any one has face a similar problem I would really appreciate if you can help or point me to any information that could help.如果有人遇到类似的问题,如果您能提供帮助或向我指出任何可以提供帮助的信息,我将不胜感激。

This is the error I am getting: The store type 'Varchar(100)' could not be found in the SqlServer provider manifest这是我得到的错误:在 SqlServer 提供程序清单中找不到存储类型“Varchar(100)”

This is because it's invalid, as mentioned in my comment above, you can't pass the length of the type in HasColumnType .这是因为它是无效的,正如我在上面的评论中提到的,你不能在HasColumnType中传递类型的长度。 To fix this please see below.要解决此问题,请参见下文。

public EntityAConfiguration()
        {
            this.ToTable("EntityA")
                .HasKey(c => c.Id);

            this.Property(c => c.Id)
                .HasColumnName("Id")
                .HasColumnType("INT")
                .IsRequired();

            this.Property(c => c.Name)
                .HasColumnName("Name")
                .HasColumnType("VARCHAR")
                .HasMaxLength(40)
                .IsRequired();

            this.Property(c => c.Description)
                .HasColumnName("Description")
                .HasColumnType("VARCHAR")
                .HasMaxLength(100)
                .IsRequired();

            this.Property(c => c.IsActive)
                .HasColumnName("IsActive")
                .HasColumnType("bit");
        }

You will now see a new property called HasMaxLength , this is where you define the length.您现在将看到一个名为HasMaxLength的新属性,这是您定义长度的地方。 The HasColumnType is only the type definition, nothing more. HasColumnType只是类型定义,仅此而已。

Of course, this is a matter of taste, but I prefer the option using attributes.当然,这是个人喜好问题,但我更喜欢使用属性的选项。 In this case, all the code is in one place.在这种情况下,所有代码都在一个地方。

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public class EntityA: BaseEntity
{
    /// <summary>
    /// Gets or sets Contact identifier
    /// </summary>
    public int Id { get; set; }

    [Column(TypeName = "VARCHAR")]
    [StringLength(40)]
    public string Name { get; set; }

    [Column(TypeName = "VARCHAR")]
    [StringLength(100)]
    public string Description { get; set; }

    public bool IsActive { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Net / IIS服务器错误-找不到CodeDOM提供程序类型 - .Net / IIS server error - CodeDOM Provider Type Could Not Be Found Win Store应用程式中&#39;找不到类型或名称空间名称&#39;ICloneable&#39; - 'The type or namespace name 'ICloneable' could not be found' in win store app 无法为角色提供者加载类型 - Could not load type for role provider 找不到 Microsoft.SqlServer.Types 版本 10 或更高版本 - Microsoft.SqlServer.Types' version 10 or higher could not be found 找不到Microsoft.SqlServer.Management.Smo.Restore错误 - Microsoft.SqlServer.Management.Smo.Restore could not be found error 无法找到程序集“Microsoft.SqlServer.Types”版本10或更高版本 - Assembly 'Microsoft.SqlServer.Types' version 10 or higher could not be found 找不到类型/名称空间 - Type/ NameSpace could not be found 找不到类型秒表 - Type Stopwatch could not be found 如何将字典存储在 SQLServer 用户定义类型中? - How would one store a Dictionary in a SQLServer User-Defined Type? 实体框架如何在生成的 SQL (SQLServer 2005) Nvarchar vs Varchar 中指定参数类型 - Entity Framework How to specify paramter type in generated SQL (SQLServer 2005) Nvarchar vs Varchar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM