2

Я работаю над несколько простым приложением InventoryTracker MVC5, в котором у меня возникают некоторые проблемы с получением моей LocalDatabase до Seed().MVC EntityFramework Code-First Migrations - Seed() Неудача с AmbiguousMatchException?

Когда я запускаю команду update-database на моем коде ниже, я получаю исключение следующим образом на линии context.INV_Assets.AddRange(invAssets):

An exception of type 'System.Reflection.AmbiguousMatchException' occurred in mscorlib.dll but was not handled in user code. Additional information: Ambiguous match found.

Кто-нибудь есть мысли о том, что в моем INV_Assets - Seed() вызывает проблему? Не существует InnerException, и я не могу получить больше подробностей об этом.

AmibguousMatchException


КОД:

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

    protected override void Seed(InventoryTracker.DAL.InventoryTrackerContext context) 
    { 
     if (System.Diagnostics.Debugger.IsAttached == false) 
     { 
      System.Diagnostics.Debugger.Launch(); 
     } 

     List<INV_Locations> invLocs = getLocations(); 
     context.INV_Locations.AddRange(invLocs); 
     context.SaveChanges(); 
     List<INV_Manufacturers> invManufacturers = getManufacturers(); 
     context.INV_Manufacturers.AddRange(invManufacturers); 
     context.SaveChanges(); 
     List<INV_Models> invModels = getModels(); 
     context.INV_Models.AddRange(invModels); 
     context.SaveChanges(); 
     List<INV_Statuses> invStatuses = getStatuses(); 
     context.INV_Statuses.AddRange(invStatuses); 
     context.SaveChanges(); 
     List<INV_Types> invTypes = getTypes(); 
     context.INV_Types.AddRange(invTypes); 
     context.SaveChanges(); 
     List<INV_Vendors> invVendors = getVendors(); 
     context.INV_Vendors.AddRange(invVendors); 
     context.SaveChanges(); // Everything OK to this point. 

     List<INV_Assets> invAssets = getAssets(); 
     context.INV_Assets.AddRange(invAssets); // System.Reflection.AmbiguousMatchException! 

     context.SaveChanges(); 
    } 

    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), 
       acquired_date = DateTime.ParseExact("10212014", "MMddyyyy", CultureInfo.InvariantCulture), 
       disposed_date = null, 
       created_by = "Admin", 
       created_date = DateTime.Now, 
       modified_by = "Admin", 
       modified_date = DateTime.Now, 
       Location_Id = 1, 
       Manufacturer_Id = 1, 
       Model_Id = 1, 
       Status_Id = 2, 
       Type_Id = 3, 
       Vendor_Id = 3 
      } 
     }; 
     return testAssets; 
    } 

EDIT:

INV_Assets.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using GridMvc.DataAnnotations; 
using System.Web.Mvc; 
using InventoryTracker.Models; 

namespace InventoryTracker.Models 
{ 
    [GridTable(PagingEnabled = true, PageSize = 30)] 
    public class INV_Assets 
    { 
     // Setting GridColumn Annotations allows you to use AutoGenerateColumns on view to auto create the Grid based on the model. 


     public int Id { get; set; } 

     public int Model_Id { get; set; } 
     [ForeignKey("Model_Id")] 
     public virtual INV_Models model_id { get; set; } 

     [Required] 
     public int Manufacturer_Id { get; set; } 
     [ForeignKey("Manufacturer_Id")] 
     public virtual INV_Manufacturers manufacturer_id { get; set; } 

     [Required] 
     public int Type_Id { get; set; } 
     [ForeignKey("Type_Id")] 
     public virtual INV_Types type_id { get; set; } 

     [Required] 
     public int Location_Id { get; set; } 
     [ForeignKey("Location_Id")] 
     public virtual INV_Locations location_id { get; set; } 

     public int Vendor_Id { get; set; } 
     [ForeignKey("Vendor_Id")] 
     public virtual INV_Vendors vendor_id { get; set; } 

     [Required] 
     public int Status_Id { get; set; } 
     [ForeignKey("Status_Id")] 
     public virtual INV_Statuses status_id { get; set; } 

     public string ip_address { get; set; } 

     public string mac_address { get; set; } 

     public string note { get; set; } 
     public string owner { get; set; } 
     public decimal cost { get; set; } 
     public string po_number { get; set; } 


     public int invoice_number{ get; set; } 

     [Required] 
     public string serial_number { get; set; } 

     [Required] 
     public string asset_tag_number { get; set; } 

     [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")] 
     public DateTime? acquired_date { get; set; } 

     [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")] 
     public DateTime? disposed_date { get; set; } 

     [Required] 
     [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")] 
     public DateTime created_date { get; set; } 

     [Required] 
     public string created_by { get; set; } 

     [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")] 
     public DateTime modified_date { get; set; } 

     public string modified_by { get; set; } 

     // Flag to specify if item is available? (Not signed out, not auctioned, recycled, etc.) 
     //public bool available { get; set; } 
    } 
} 

EDIT2:

Полный результат в Package Manager Console от ввода команды update-database в результате Ambiguous Match found:

PM> update-database 
Specify the '-Verbose' flag to view the SQL statements being applied to the target database. 
No pending explicit migrations. 
Running Seed method. 
System.Reflection.AmbiguousMatchException: Ambiguous match found. 
    at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) 
    at System.Type.GetProperty(String name, BindingFlags bindingAttr) 
    at System.Linq.Expressions.Expression.PropertyOrField(Expression expression, String propertyOrFieldName) 
    at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.CreateBaseGetter(Type declaringType, PropertyInfo propertyInfo) 
    at System.Data.Entity.Core.Objects.Internal.PocoPropertyAccessorStrategy.GetNavigationPropertyValue(RelatedEnd relatedEnd) 
    at System.Data.Entity.Core.Objects.Internal.EntityWrapper`1.GetNavigationPropertyValue(RelatedEnd relatedEnd) 
    at System.Data.Entity.Core.Objects.EntityEntry.FixupFKValuesFromNonAddedReferences() 
    at System.Data.Entity.Core.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName) 
    at System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity) 
    at System.Data.Entity.Internal.Linq.InternalSet`1.<AddRange>b__f(Object entity) 
    at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action`1 action, EntityState newState, IEnumerable entities, String methodName) 
    at System.Data.Entity.Internal.Linq.InternalSet`1.AddRange(IEnumerable entities) 
    at System.Data.Entity.DbSet`1.AddRange(IEnumerable`1 entities) 
    at InventoryTracker.Migrations.Configuration.Seed(InventoryTrackerContext context) in c:\James-Projects\InventoryTracker\InventoryTracker\Migrations\Configuration.cs:line 91 
    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) 
Ambiguous match found. 

ответ

3

Ваш вопрос находится в очень похожих имен свойств. Например, в INV_Assets . вы имеете

public int Model_Id { get; set; } 

[ForeignKey("Model_Id")] 
public virtual INV_Models model_id { get; set; } 

Оба из них по имени почти одинаково разница только в первом случае символ на основе трассировки стека и Entity источника рамочным он делает такую ​​вещь в EntityProxyFactory классе:.

public virtual Func<object, object> CreateBaseGetter(Type declaringType, PropertyInfo propertyInfo) 
{ 
    Func<object, object> nonProxyGetter = 
     ((Expression<Func<object, object>>) (instance 
      => 
       Expression.PropertyOrField(
        (Expression) Expression.Convert(instance, declaringType), 
        propertyInfo.Name)) 
     ).Compile(); 

Оно передаёт имя свойства как строку. И Reflection не может понять, какое свойство он должен использовать, потому что есть два свойства с одним и тем же именем и разным случаем.

Вот пример программа с той же логикой, что и терпит неудачу с тем же Ambiguous match found. исключения - https://dotnetfiddle.net/BUIAwK

Как обычно ссылаются объект с именем с ID в имени свойства, так что вам просто нужно обновить код

public int Model_Id { get; set; } 

[ForeignKey("Model_Id")] 
public virtual INV_Models Model { get; set; } 
+1

Большое спасибо Sergey! Вот и все :) –

+0

Добро пожаловать :) –

1

Здравствуйте снова Аналитическое Lunatic :)

Хорошо, если вы смотрите здесь http://msdn.microsoft.com/en-us/library/system.reflection.ambiguousmatchexception%28v=vs.110%29.aspx Затем вы увидите эту цитату: «Исключение, которое вызывается, когда привязка к члену приводит к тому, что более одного элемента соответствуют критериям привязки. Этот класс не может наследоваться.»

У вас есть несколько свойств в INV_Assets, что мы не можем видеть? Можете ли вы показать нам класс?

+0

Здравствуйте! Конечно, я добавил в свой EDIT 'INV_Assets.cs' выше. –

+0

@Sergey уже ответил на этот вопрос. Работает ли он сейчас? –

+0

Да, Сергей получил это. Спасибо за вашу помощь! –

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