Mik*_*ePR 2 c# sql-server asp.net-mvc entity-framework entity-framework-6
我正在开发一个 MVC 4 项目,该项目使用 EF 6 进行与数据库 (SQL Server) 之间的所有操作。我必须进行一些重构才能使用更明确的 Fluent API。基本上,现在项目中的每个实体都有其对应的所有映射的 Fluent API 代码。
当我尝试从包管理器控制台运行 EF 命令时,问题就出现了。这是我收到的错误: 在 SqlServer 提供程序清单中找不到存储类型“Varchar(100)”
这是错误的完整日志:
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)
Run Code Online (Sandbox Code Playgroud)
我一直在阅读很多博客/帖子如何解决这个问题,但它们似乎都不起作用。这些是我尝试过的一些事情:
更新 这是实体的代码:
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; }
}
Run Code Online (Sandbox Code Playgroud)
在单独的类中使用 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");
}
}
Run Code Online (Sandbox Code Playgroud)
BaseEntity类只有两个属性:CreatedDate、UpdatedDate。我在需要存储该信息的所有实体中使用该类。
在这一点上,我没有想法如何解决这个问题。如果有人遇到类似的问题,如果您能提供帮助或向我指出任何可能有帮助的信息,我将不胜感激。
这是我收到的错误:在 SqlServer 提供程序清单中找不到存储类型“Varchar(100)”
这是因为它是无效的,正如我上面的评论中提到的,您不能在HasColumnType. 要解决此问题,请参见下文。
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");
}
Run Code Online (Sandbox Code Playgroud)
您现在将看到一个名为 的新属性HasMaxLength,这是您定义长度的地方。这HasColumnType只是类型定义,仅此而已。