【问题标题】:Error in Entity Framework 6.0.2 using "Migration"使用“迁移”的实体框架 6.0.2 中的错误
【发布时间】:2013-12-18 15:59:52
【问题描述】:

我在 ASP.NET MVC 4 中有一个项目,我正在使用 Entity Framework 6.0.2 进行数据持久性。创建所有实体(“模型”)后,使用数据注释,我使用“迁移”创建包,但在 SQL Server 2012 中数据库不是 criano 后,显示 Nuget 消息“序列不包含匹配元素”

“连接字符串”已配置,SQL Server 2012 处于活动状态。

我使用了命令:

PM> Enable-Migrations


PM>Checking if the context targets an existing database...
System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name)
   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__DisplayClass1.<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.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, 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.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Sequence contains no matching element

【问题讨论】:

  • Eneble-Migrations 我希望这是复制粘贴失败或这里的错字
  • 您是否正在将任何数据从一个版本迁移到另一个版本?如果是这样,您能否发布该代码?
  • 这对我来说似乎是一个错误 - 你可以使用 entityframework.codeplex.com/WorkItem/Create 归档这个吗?请提供重现。

标签: entity-framework asp.net-mvc-4 migration nuget


【解决方案1】:

首先认为这是 Visual Studio 中的错误,甚至是对实体框架安装的类或引用中的错误。 唯一的问题是“双重”属性。使用了错误的符号。 前任: 而不是使用:

        [Required(ErrorMessage = "Informe a Nota do Aluno")]
        [Column("NotaAluno", TypeName = "Decimal(3,2)")]
        [DisplayName("Nota do Aluno:*")]
        public Double dNotaAluno { get; set; }

使用这个:

        [DisplayFormat(DataFormatString = "{0:n2}",
            ApplyFormatInEditMode = true,
            NullDisplayText = "Sem Nota")]
        [Range(0, 100, ErrorMessage = "A nota deverá ser entre 0 a 100.")]
        [Required(ErrorMessage = "Informe a Nota do Aluno")]
        [Column("NotaAluno", TypeName = "decimal")]
        [DisplayName("Nota do Aluno:*")]
        public decimal dNota { get; set; }

小问题,在创建类和尊重SQL Server的原始类型时缺乏注意。 谢谢。

【讨论】:

  • 认为“序列不包含匹配元素”的错误信息足以用于调试。
【解决方案2】:

我在尝试从 sql server 迁移到 mysql 时遇到了类似的问题,我发现这是因为使用 datetime2。对我有用的建议修复:使用普通的旧 DateTime

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 2013-09-17
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多