AmbiguousMatchException when selecting from DbContext

ambiguous c# entity-framework-core

Question

I've seen a lot of questions about how to debug an AmbiguousMatchException encountered when updating or adding a record to a context, or when there are multiple overloads to a method, but I've found nothing so far about issues found when selecting from a context.

Suppose this is my class:

public class Foo
{
    public decimal IdNumber {get; set;}

    //...
}

And the mapping and context:

public class FoosMapping : EntityTypeConfigurationBase /*custom class*/, IEntityTypeConfiguration<Foo>
{
    public void Configure(EntityTypeBuilder<Foo> builder)
    {
        //Table
        builder.ToTable("Foos");

        //Key
        builder.HasKey(key => new {key.IdNumber}).HasName("FoosPk");

        //Fields
        PropertyBuilder<decimal> idField = builder.Property(x => x.IdNumber);
        idField.HasColumnName("IdNumber");
        if (this.ActiveProvider.IndexOf("SqlServer", StringComparison.InvariantCultureIgnoreCase) >= 0)
            idField.HasColumnType("numeric(6,0)");
        //...
    }
}
/***************************************************************************/
public class FooContext
{
    public DbSet<Foo> Foos {get; set;}

    //...
}

Now, I have a reader class that's supposed to pick one Foo based on its IdNumber. This should be doable with a straightforward call in the reader to this.Context.Foos.SingleOrDefault(), but instead it started giving me an AmbiguousMatchException. I even broke it out like this...

20: Foo[] foos = this.Context.Foos.ToArray();
21: Foo result = foos.SingleOrDefault(x => x.IdNumber == idNumber);

...and the exception appears when initializing the array variable. The text Foos is only found in the three classes mentioned above, plus a writer class and an adapter class. The Foo class is only referenced in those three files and an Autofac binding.

Is my problem the idField in the mapping, the fact that the DB table is also called "Foos", or something else?

EDIT: The stack trace on the exception:

"   at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)"

The top of the call stack:

System.Core.dll!System.Linq.Buffer<MyProject.Data.Foo>.Buffer(System.Collections.Generic.IEnumerable<MyProject.Data.Foo> source)

System.Core.dll!System.Linq.Enumerable.ToArray<MyProject.Data.Foo>(System.Collections.Generic.IEnumerable<MyProject.Data.Foo> source)

> MyProject.Entity.dll!MyProject.Entity.Readers.FooReader.Read(decimal idNumber) Line 20
1
0
7/30/2018 7:43:50 PM

Popular Answer

For future users encountering similar problems and for whom other solutions aren't working, this issue was due to a new version of EF Core being installed, which was not playing well with our existing dependency injection framework. The Powers-That-Be decided to roll back to an older EF Core version for the time being, and everything works now.

0
8/3/2018 2:49:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow