0

Я был следующий ниже учебник в попытке настроить код первой EntityFramework Миграции в моем MVC5 App, а затем Seed мою базу данных с некоторыми образцами данных для использования: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-applicationMVC EntityFramework Code-First Migration выполняется, но Seed не вступает в силу?

Мой проект теперь успешно строит, и я могу пройти через все шаги, чтобы изменить свою первоначальную базу данных:

- update-database -targetmigration:"0" -force -verbose 
- *Delete Current Migrations 
- add-migration InitialCreate 
- update-database 

Когда последняя команда завершает мой Seed метод предположительно является RAN (и я могу видеть, что таблицы создается с помощью параметра -verbose). Однако в моей таблице INV_Assets нет записи, где должен быть один, указанный моим текущим посевом.

Может кто-нибудь предложить какой-либо вклад в действие, почему мой метод Seed не работает, хотя кажется, что работает?


InventoryTracker.DAL.InventoryInitializer.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using InventoryTracker.Models; 
using InventoryTracker.DAL; 
using WebMatrix.WebData; 

namespace InventoryTracker.DAL 
{ 
    public class InventoryInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<InventoryTrackerContext> 
    { 
     InventoryTrackerContext context = new InventoryTrackerContext(); 

     protected override void Seed(InventoryTrackerContext context) 
     { 
      List<INV_Locations> invLocs = getLocations(); 
      invLocs.ForEach(s => context.INV_Locations.Add(s)); 
      List<INV_Manufacturers> invManufacturers = getManufacturers(); 
      invManufacturers.ForEach(s => context.INV_Manufacturers.Add(s)); 
      List<INV_Models> invModels = getModels(); 
      invModels.ForEach(s => context.INV_Models.Add(s)); 
      List<INV_Statuses> invStatuses = getStatuses(); 
      invStatuses.ForEach(s => context.INV_Statuses.Add(s)); 
      List<INV_Types> invTypes = getTypes(); 
      invTypes.ForEach(s => context.INV_Types.Add(s)); 
      List<INV_Vendors> invVendors = getVendors(); 
      invVendors.ForEach(s => context.INV_Vendors.Add(s)); 

      List<INV_Assets> invAssets = getAssets(); 
      invAssets.ForEach(s => context.INV_Assets.Add(s)); 

      context.SaveChanges(); 
     } 

     #region Seed Assets 
     private List<INV_Assets> getAssets() 
     { 
      List<INV_Assets> testAssets = new List<INV_Assets> 
      { 
       new INV_Assets 
       { 
        Id = 1, 
        ip_address = "10.10.135.38", 
        mac_address = "10.10.177.44", 
        note = "", 
        owner = "John Smith", 
        cost = 35, 
        po_number = "G348", 
        invoice_number = 1447, 
        serial_number = "JX14582Y", 
        asset_tag_number = "293548195023", 
        acquired_date = Convert.ToDateTime(10212014), 
        disposed_date = null, 
        created_by = "Admin", 
        created_date = DateTime.Now, 
        location_id = 1, 
        manufacturer_id = 1, 
        model_id = 1, 
        status_id = 2, 
        type_id = 3, 
        vendor_id = 3 
       } 
      }; 
      return testAssets; 
     } 
     #endregion 

     [Seed Locations] 
     [Seed Manufacturers] 

     #region Seed Models 
     private List<INV_Models> getModels() 
     { 
      List<INV_Models> testModels = new List<INV_Models> 
      { 
       new INV_Models 
       { 
        Id = 1, 
        model_description = "XTERAV12", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Models 
       { 
        Id = 2, 
        model_description = "5330", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Models 
       { 
        Id = 1, 
        model_description = "Sunblade 6000", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       } 
      }; 
      return testModels; 
     } 
     #endregion 

     [Seed Statuses] 
     [Seed Types] 
     [Seed Vendors] 
    } 
    } 

InventoryTracker.DAL.InventoryTrackerContext.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using InventoryTracker.Models; 
using System.Data.Entity; 
using System.Data.Entity.ModelConfiguration.Conventions; 

namespace InventoryTracker.DAL 
{ 
    public class InventoryTrackerContext : DbContext 
    { 
     public InventoryTrackerContext() 
      : base("InventoryTrackerContext") 
     { 

     } 

     public DbSet<INV_Assets> INV_Assets { get; set; } 
     public DbSet<INV_Models> INV_Models { get;set; } 
     public DbSet<INV_Manufacturers> INV_Manufacturers { get;set; } 
     public DbSet<INV_Types> INV_Types { get; set; } 
     public DbSet<INV_Locations> INV_Locations { get; set; } 
     public DbSet<INV_Vendors> INV_Vendors { get; set; } 
     public DbSet<INV_Statuses> INV_Statuses { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
     } 
    } 
} 

Web.config:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="InventoryTrackerContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\InventoryTrackerDBDev.mdf;Initial Catalog=InventoryTrackerDBDev;Integrated Security=True" providerName="System.Data.SqlClient" /> 
    <!--<add name="PRISMContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=PrismDBdev5;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\PrismDBdev5.mdf" providerName="System.Data.SqlClient" />--> 
    </connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <!-- Active Directory Authentication - http://www.asp.net/mvc/overview/older-versions-1/security/authenticating-users-with-windows-authentication-cs --> 
    <authentication mode="Windows" /> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web" /> 
    </httpModules> 
    <pages> 
     <namespaces> 
     <add namespace="GridMvc" /> 
     </namespaces> 
    </pages></system.web> 
    <system.webServer> 
    <modules> 
     <remove name="FormsAuthentication" /> 
     <remove name="ApplicationInsightsWebTracking" /> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web" preCondition="managedHandler" /> 
    </modules> 
    <validation validateIntegratedModeConfiguration="false" /> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebMatrix.Data" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
     <contexts> 
      <context type="InventoryTracker.DAL.InventoryTrackerContext, InventoryTracker"> 
       <databaseInitializer type="InventoryTracker.DAL.InventoryInitializer, InventoryTracker" /> 
      </context> 
     </contexts> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 


EDIT:

Ok, так что, похоже, куда я шел неправильно настраивал мое семя в неправильном месте. Он должен был все время находиться в файле InventoryTracer.Migrations.Configuration.cs ... который не содержал семена в него ... поэтому я не получал никаких вставленных данных после семян ...:/

Я сейчас переместил посев в соответствующий метод семян в файле Configuration.cs, но я получаю сообщение об ошибке: Invalid cast from 'Int32' to 'DateTime'.

полный след от Package Manager Console выглядит следующим образом:

Running Seed method. 
System.InvalidCastException: Invalid cast from 'Int32' to 'DateTime'. 
    at System.Int32.System.IConvertible.ToDateTime(IFormatProvider provider) 
    at System.Convert.ToDateTime(Int32 value) 
    at InventoryTracker.Migrations.Configuration.getAssets() in c:\James-Projects\InventoryTracker\InventoryTracker\Migrations\Configuration.cs:line 57 
    at InventoryTracker.Migrations.Configuration.Seed(InventoryTrackerContext context) in c:\James-Projects\InventoryTracker\InventoryTracker\Migrations\Configuration.cs:line 47 
    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) 
Invalid cast from 'Int32' to 'DateTime'. 

Я не совсем уверен, где ошибка приведения типов однако, поскольку строки 47/57 моего Configuration.cs соответственно:

47 - List<INV_Assets> invAssets = getAssets(); 
57 - List<INV_Assets> testAssets = new List<INV_Assets> 

Мое предположение, что оно вращается вокруг линии 71 - acquired_date = Convert.ToDateTime(10212014),.

У кого-нибудь есть мысли о том, как разрешить это?


Конфигурация.CS:

namespace InventoryTracker.Migrations 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Data.Entity; 
    using System.Data.Entity.Migrations; 
    using System.Linq; 
    using System.Web; 
    using InventoryTracker.Models; 
    using InventoryTracker.DAL; 
    using WebMatrix.WebData; 

    internal sealed class Configuration : DbMigrationsConfiguration<InventoryTracker.DAL.InventoryTrackerContext> 
    { 
     public Configuration() 
     { 
      AutomaticMigrationsEnabled = false; 
     } 

     protected override void Seed(InventoryTracker.DAL.InventoryTrackerContext context) 
     { 
      // This method will be called after migrating to the latest version. 

      // You can use the DbSet<T>.AddOrUpdate() helper extension method 
      // to avoid creating duplicate seed data. E.g. 
      // 
      // context.People.AddOrUpdate(
      //  p => p.FullName, 
      //  new Person { FullName = "Andrew Peters" }, 
      //  new Person { FullName = "Brice Lambson" }, 
      //  new Person { FullName = "Rowan Miller" } 
      // ); 
      // 
      List<INV_Locations> invLocs = getLocations(); 
      invLocs.ForEach(s => context.INV_Locations.Add(s)); 
      List<INV_Manufacturers> invManufacturers = getManufacturers(); 
      invManufacturers.ForEach(s => context.INV_Manufacturers.Add(s)); 
      List<INV_Models> invModels = getModels(); 
      invModels.ForEach(s => context.INV_Models.Add(s)); 
      List<INV_Statuses> invStatuses = getStatuses(); 
      invStatuses.ForEach(s => context.INV_Statuses.Add(s)); 
      List<INV_Types> invTypes = getTypes(); 
      invTypes.ForEach(s => context.INV_Types.Add(s)); 
      List<INV_Vendors> invVendors = getVendors(); 
      invVendors.ForEach(s => context.INV_Vendors.Add(s)); 

      List<INV_Assets> invAssets = getAssets(); 
      invAssets.ForEach(s => context.INV_Assets.Add(s)); 

      context.SaveChanges(); 
     } 


     #region Seed Assets 
     private List<INV_Assets> getAssets() 
     { 
      List<INV_Assets> testAssets = new List<INV_Assets> 
      { 
       new INV_Assets 
       { 
        Id = 1, 
        ip_address = "10.10.135.38", 
        mac_address = "10.10.177.44", 
        note = "", 
        owner = "John Smith", 
        cost = 35, 
        po_number = "G348", 
        invoice_number = 1447, 
        serial_number = "JX14582Y", 
        asset_tag_number = "293548195023", 
        acquired_date = Convert.ToDateTime(10212014), 
        disposed_date = null, 
        created_by = "Admin", 
        created_date = DateTime.Now, 
        Location_Id = 1, 
        Manufacturer_Id = 1, 
        Model_Id = 1, 
        Status_Id = 2, 
        Type_Id = 3, 
        Vendor_Id = 3 
       } 
      }; 
      return testAssets; 
     } 
     #endregion 

     #region Seed Locations 
     private List<INV_Locations> getLocations() 
     { 
      List<INV_Locations> testLocations = new List<INV_Locations> 
      { 
       new INV_Locations 
       { 
        Id = 1, 
        location_dept = "IT", 
        location_room = "Server", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Locations 
       { 
        Id = 2, 
        location_dept = "Break Room", 
        location_room = "Kitchen", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Locations 
       { 
        Id = 3, 
        location_dept = "Accounting", 
        location_room = "Conference", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       } 
      }; 
      return testLocations; 
     } 
     #endregion 

     #region Seed Manufacturers 
     private List<INV_Manufacturers> getManufacturers() 
     { 
      List<INV_Manufacturers> testManufacturers = new List<INV_Manufacturers> 
      { 
       new INV_Manufacturers 
       { 
        Id = 1, 
        manufacturer_description = "Samsung", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Manufacturers 
       { 
        Id = 2, 
        manufacturer_description = "MITEL", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Manufacturers 
       { 
        Id = 1, 
        manufacturer_description = "Oracle", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       } 
      }; 
      return testManufacturers; 
     } 
     #endregion 

     #region Seed Models 
     private List<INV_Models> getModels() 
     { 
      List<INV_Models> testModels = new List<INV_Models> 
      { 
       new INV_Models 
       { 
        Id = 1, 
        model_description = "XTERAV12", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Models 
       { 
        Id = 2, 
        model_description = "5330", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Models 
       { 
        Id = 1, 
        model_description = "Sunblade 6000", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       } 
      }; 
      return testModels; 
     } 
     #endregion 

     #region Seed Statuses 
     private List<INV_Statuses> getStatuses() 
     { 
      List<INV_Statuses> testStatuses = new List<INV_Statuses> 
      { 
       new INV_Statuses 
       { 
        Id = 1, 
        status_description = "AVAILABLE", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Statuses 
       { 
        Id = 2, 
        status_description = "SIGNEDOUT", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Statuses 
       { 
        Id = 3, 
        status_description = "RECYCLED", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Statuses 
       { 
        Id = 4, 
        status_description = "AUCTIONED", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       } 
      }; 
      return testStatuses; 
     } 
     #endregion 

     #region Seed Types 
     private List<INV_Types> getTypes() 
     { 
      List<INV_Types> testTypes = new List<INV_Types> 
      { 
       new INV_Types 
       { 
        Id = 1, 
        type_description = "Server", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Types 
       { 
        Id = 2, 
        type_description = "IP Phone", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Types 
       { 
        Id = 1, 
        type_description = "Monitor", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       } 
      }; 
      return testTypes; 
     } 
     #endregion 

     #region Seed Vendors 
     private List<INV_Vendors> getVendors() 
     { 
      List<INV_Vendors> testVendors = new List<INV_Vendors> 
      { 
       new INV_Vendors 
       { 
        Id = 1, 
        vendor_name = "Oracle", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Vendors 
       { 
        Id = 2, 
        vendor_name = "Centriq", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       }, 
       new INV_Vendors 
       { 
        Id = 1, 
        vendor_name = "Samsung", 
        created_by = "Admin", 
        created_date = DateTime.Now 
       } 
      }; 
      return testVendors; 
     } 
     #endregion 

    } 
} 

EDIT2:

Я думаю я получил Invalid cast from 'Int32' to 'DateTime'. позаботиться о путем изменения acquired_date = Convert.ToDateTime(10212014) к acquired_date = DateTime.ParseExact("10212014", "MMddyyyy", CultureInfo.InvariantCulture), но теперь у меня есть новая ошибка.

Когда я запустил команду update-database, мой метод Seed теперь завершился с ошибкой Ambiguous match found. Размещая следующий код в верхней части моего Seed() я пытался получить более подробную информацию:

 if (System.Diagnostics.Debugger.IsAttached == false) 
     { 
      System.Diagnostics.Debugger.Launch(); 
     } 

Когда код достигает invAssets.ForEach(s => context.INV_Assets.Add(s)); отладчик, который я начал уловы с An exception of type 'System.Reflection.AmbiguousMatchException' occurred in mscorlib.dll but was not handled in user code. Additional information: Ambiguous match found.

Кто-нибудь есть мысли о том, что вызывая это? Я видел потенциал ошибки Ambiguous: я добавляю несколько тестовых записей, но в это время у меня есть только одна запись, высеваемая для INV_Assets.

После Soren Gustenhoff предложений, я изменил Foreach => Add() использовать .AddRange() и добавил предварительный context.SaveChanges() перед getAssets() называется, но это привело лишь в следующем, когда AddRange(invTypes) называется?

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code 

Additional information: An error occurred while updating the entries. See the inner exception for details. 

КОД: context.INV_Types.AddRange (invTypes); // исключение типа 'System.Data.Entity.Infrastructure.DbUpdateException //context.SaveChanges(); Список invVendors = getVendors(); context.INV_Vendors.AddRange (invVendors); context.SaveChanges(); // Комментируем эти флаги [getVendors()] с неоднозначной ошибкой?

 List<INV_Assets> invAssets = getAssets(); 
     context.INV_Assets.AddRange(invAssets); 

     context.SaveChanges(); 

Возвращаясь к только имея один context.SaveChanges() в конце концов, моя getVendors() ослабевает с ныне AmbiguousMatchException первым ???

+0

Что '10212014' должен представлять? Если это дата формата 'MMddyyyy', возможно, более подходящим может быть изменение в' Convert.ToDateTime («2014-10-21»). –

+0

Спасибо за ответ Брендан. Если вы проверите мое Редактирование, я думаю, что у меня проблема с данными, но у меня возникла новая проблема. –

ответ

3

Вы настраиваете отношения в своих INV_Assets, которые еще не созданы.

EF Контекст не будет создавать первичные ключи до тех пор пока вы запустите SaveChanges

я бы порекомендовал вам сделать это

List<INV_Types> invTypes = getTypes(); 
invTypes.ForEach(s => context.INV_Types.Add(s)); 
context.INV_Types.AddRange(invTypes); 
List<INV_Vendors> invVendors = getVendors(); 
context.INV_Vendors.AddRange(invVendors); 

context.SaveChanges(); 

List<INV_Assets> invAssets = getAssets(); 
context.INV_Assets.AddRange(invAssets); 

context.SaveChanges(); 

Btw. Обратите внимание на addRange вместо foreach -> add.

Ps. Для отладки метод семян, увидеть это: Debugging Package Manager Console Update-Database Seed Method

Я надеюсь, что помог :)

+0

Сорен, спасибо за ответ. Я сделал ваши предложения (очень ценю ссылку отладки!), Но у меня возникают новые проблемы. Я сделал EDIT выше, чтобы отразить изменения, но так как я превысил лимит символов для сообщения, я создал новый вопрос для конкретной новой проблемы, если вы хотите взглянуть? https://stackoverflow.com/questions/27802953/mvc-entityframework-code-first-migrations-seed-failing-with-dbupdateexceptio –

+0

Сорен, быстрый комментарий. У меня все еще есть некоторые проблемы с моим 'Seed()' (все работает, за исключением посещений 'INV_Assets', но я заметил что-то странное. Когда я использую' .AddRange() ', как вы сказали, каждый посев другие таблицы встречаются 4 раза (вместо 3 отдельных записей, я заканчиваю с 4 копиями одного и того же 3 на 12)? Когда я переключил свой код на использование метода 'Foreach (=> .Add()), другие записи таблицы заполняются так, как я ожидал. Мысли о том, почему это может произойти? Новая проблема: https://stackoverflow.com/questions/27804300/ –

+0

Вы нашли решение этой проблемы? Если да, пожалуйста, отметьте ответ как принятый. –

Смежные вопросы