使用EF6和.NET 4.6.1在数据库迁移期间获取多个错误

时间:2016-06-11 05:25:09

标签: entity-framework c#-4.0 ef-migrations

获得以下错误:

“一个或多个实体的验证失败。有关详细信息,请参阅'EntityValidationErrors'属性。”在Configuration.cs的第89行

第89行只是context.SaveChanges();,位于类文件的末尾:

namespace Model.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Model.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(ApplicationDbContext context)
        {
            SeedData.AdditionalSeed _AdditionalSeed = new SeedData.AdditionalSeed();

            _AdditionalSeed.Seed_StateOrProvince(context);
            _AdditionalSeed.Seed_CountryOrRegion(context);
            _AdditionalSeed.Seed_ContactTypes(context);
            _AdditionalSeed.Seed_CurrencyCodes(context);

            _AdditionalSeed.Seed_Preferences_CalendarSettings_CalendarView(context);
            _AdditionalSeed.Seed_Preferences_CalendarSettings_WeeklyView(context);
            _AdditionalSeed.Seed_Preferences_CalendarSettings_WhatToShow(context);
            _AdditionalSeed.Seed_Preferences_Calendar_FuturePastSettings(context);
            _AdditionalSeed.Seed_Preferences_ItemsAndInventory_AdvancedInventory_Barcodes_CopyBarcodeFrom(context);
            _AdditionalSeed.Seed_Preferences_ItemsAndInventory_UnitOfMeasureType(context);
            _AdditionalSeed.Seed_Preferences_PayrollEmployees_EmployeeDefaults_PayFrequency(context);
            _AdditionalSeed.Seed_Preferences_PayrollEmployees_EmployeeDefaults_SickAndVacationDefaults_AccuralPeriod(context);
            _AdditionalSeed.Seed_Preferences_PayrollEmployees_EmployeeDefaults_Taxes_FilingStatus(context);
            _AdditionalSeed.Seed_Preferences_ReportsAndGraphs_ReportPreferences_DatePreparedFormat(context);
            _AdditionalSeed.Seed_Preferences_ReportsAndGraphs_ReportPreferences_PageNumberFormat(context);
            _AdditionalSeed.Seed_Preferences_ReportsAndGraphs_ReportPreferences_PageLayout(context);
            _AdditionalSeed.Seed_Preferences_SalesAndCustomers_InvoicePackingSlip(context);
            _AdditionalSeed.Seed_Preferences_SalesAndCustomers_RoundingRules(context);
            _AdditionalSeed.Seed_Preferences_SalesAndCustomers_Shippers(context);
            _AdditionalSeed.Seed_Preferences_SalesTax_Intervals(context);
            _AdditionalSeed.Seed_Preferences_SalesTax_SalesTaxCode(context);
            _AdditionalSeed.Seed_Preferences_SendForms_SendMethod(context);
            _AdditionalSeed.Seed_Preferences_SendForms_DeliveryMethod(context);
            _AdditionalSeed.Seed_Preferences_SendForms_EmailTemplateCategoryList(context);
            _AdditionalSeed.Seed_Preferences_TimeAndExpenses_WorkWeek(context);

            _AdditionalSeed.Seed_Security_RoleMaster(context);
            context.SaveChanges();
            _AdditionalSeed.Seed_Security_AccountingRole(context);
            _AdditionalSeed.Seed_Security_BankingRole(context);
            _AdditionalSeed.Seed_Security_CentersRole(context);
            _AdditionalSeed.Seed_Security_CompanyRole(context);
            _AdditionalSeed.Seed_Security_CustomerAndReceivablesRole(context);
            _AdditionalSeed.Seed_Security_EmployeesAndPayrollRole(context);
            _AdditionalSeed.Seed_Security_FileRole(context);
            _AdditionalSeed.Seed_Security_ListsRole(context);
            _AdditionalSeed.Seed_Security_ReportsRole(context);
            _AdditionalSeed.Seed_Security_SiteSettingsRole(context);
            _AdditionalSeed.Seed_Security_TimeTrackingRole(context);
            _AdditionalSeed.Seed_Security_VendorsAndPayablesRole(context);

            _AdditionalSeed.Seed_TaxesFilingStatus(context);
            _AdditionalSeed.Seed_EmploymentType(context);
            _AdditionalSeed.Seed_EmployeeEmergencyContactType(context);
            _AdditionalSeed.Seed_EmployeeEthnicity(context);
            _AdditionalSeed.Seed_EmployeeMaritalStatus(context);
            _AdditionalSeed.Seed_Salutations(context);

            _AdditionalSeed.Seed_DefaultUnitUsedForTypes(context);
            _AdditionalSeed.Seed_UnitOfMeasureDefaultType(context);
            context.SaveChanges();
            _AdditionalSeed.Seed_UnitOfMeasureDefaultBaseUnits(context);
            context.SaveChanges();
            _AdditionalSeed.Seed_UnitOfMeasureDefaultRelatedUnits(context);
            context.SaveChanges();

            _AdditionalSeed.Seed_Terms(context);

            _AdditionalSeed.Seed_TaxForms(context);
            context.SaveChanges();
            _AdditionalSeed.Seed_TaxFormDetail(context);

            _AdditionalSeed.Seed_AccountTypes(context);
            context.SaveChanges();

            _AdditionalSeed.Seed_Industries(context);
            context.SaveChanges();

            _AdditionalSeed.Seed_OrganizationTypes(context);
            context.SaveChanges();
        }
    }
}

当我评论此行时,我会收到以下错误:

在TaxFormDetail.cs第12行

“序列包含多个元素”

此类文件的代码超过1200行,因此我只提供唯一的行:

namespace Model.Migrations.SeedData
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    public partial class AdditionalSeed
    {
        public void Seed_TaxFormDetail(ApplicationDbContext context)
        {
            context.TaxFormDetail.AddOrUpdate(
                r => new { r.TaxFormID, r.TaxCodeLineDescription },
                new TaxFormDetailModel() { TaxCodeLineNumber = null, TaxCodeLineDescription = "Schedule F: Interest expense, mortgage", TaxFormID = context.TaxForms.FirstOrDefault(r => r.TaxForm.Equals("1040")).RecordID },
                .....
                new TaxFormDetailModel() { TaxCodeLineNumber = null, TaxCodeLineDescription = "Pt I-Unrel Bus Inc: Sch C, L3-Inc-real/pers prop", TaxFormID = context.TaxForms.FirstOrDefault(r => r.TaxForm.Equals("990-T")).RecordID }
            );
        }
    }
}

此表的模型如下:

[Table(name: "TaxFormDetail")]
public class TaxFormDetailModel
{
    [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid RecordID { get; set; }

    public Guid TaxFormID { get; set; }

    public string TaxCodeLineNumber { get; set; }

    [MaxLength(255)]
    public string TaxCodeLineDescription { get; set; }
}

我已经阅读了很多关于这些错误的google和SO结果,除非我遗漏了什么,否则没有任何帮助。

修改

我唯一的错误是堆栈跟踪,因为这是由程序包管理器运行的,我没有其他任何方式获取其他任何东西:

System.InvalidOperationException: Sequence contains more than one element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](DbSet`1 set, IEnumerable`1 identifyingProperties, InternalSet`1 internalSet, TEntity[] entities)
   at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](IDbSet`1 set, Expression`1 identifierExpression, TEntity[] entities)
   at Model.Migrations.SeedData.AdditionalSeed.Seed_TaxFormDetail(ApplicationDbContext context) in Z:\_Profile Storage\Projects\Corporate.WEB\Model\Migrations\SeedData\TaxFormDetail.cs:line 12
   at Model.Migrations.Configuration.Seed(ApplicationDbContext context) in Z:\_Profile Storage\Projects\Corporate.WEB\Model\Migrations\Configuration.cs:line 81
   at System.Data.Entity.Migrations.DbMigrationsConfiguration`1.OnSeed(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.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.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Sequence contains more than one element

1 个答案:

答案 0 :(得分:0)

我找到了解决这种情况的方法。

对于&#34;一个或多个实体的验证失败。请参阅&#39; EntityValidationErrors&#39;物业详情。&#34;在Configuration.cs第89行错误,我更改了种子方法以检查该特定表(税码)的现有数据,如果该表为空,则添加记录 NOT add或更新

至于&#34;序列包含多个元素&#34;在TaxFormDetail.cs的第12行,我基本上做了同样的事情。

现在,每次执行Update-Database PM命令时,我都不会再收到任何错误。