Base solution for your next web application
Open Closed

Ambiguous match found. #1274


User avatar
0
tjackadams created

Hello,

I am running this bit of code from an MVC controller. The method has the UnitOfWork attribute and is virtual,

This is the bit of code where the exception is thrown

var roles = await _roleManager.Roles.Where(r => r.IsDefault).ToListAsync();

This is the exception that is being thrown

System.Reflection.AmbiguousMatchException occurred HResult=-2147475171 Message=Ambiguous match found. Source=mscorlib StackTrace: 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 Abp.Domain.Repositories.AbpRepositoryBase2.CreateEqualityExpressionForId(TPrimaryKey id) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Repositories\AbpRepositoryBase.cs:line 257 at Abp.Domain.Repositories.AbpRepositoryBase2.FirstOrDefault(TPrimaryKey id) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Repositories\AbpRepositoryBase.cs:line 99 at Castle.Proxies.Invocations.AbpRepositoryBase2_FirstOrDefault_30.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 54 at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 47 at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.EfRepositoryBase2Proxy_1.FirstOrDefault(Int32 id) at Abp.Domain.Repositories.AbpRepositoryBase2.Get(TPrimaryKey id) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Repositories\AbpRepositoryBase.cs:line 68 at Castle.Proxies.Invocations.AbpRepositoryBase2_Get_15.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 54 at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 47 at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.EfRepositoryBase2Proxy_1.Get(Int32 id) at Abp.MultiTenancy.TenantCache2.GetTenant(Int32 tenantId) in D:\Halil\GitHub\module-zero\src\Abp.Zero\MultiTenancy\TenantCache.cs:line 60 at Castle.Proxies.Invocations.TenantCache2_GetTenant.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 54 at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 47 at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.TenantCache2Proxy.GetTenant(Int32 tenantId) at Abp.MultiTenancy.TenantCache2.<>c__DisplayClass4_0.<Get>b__0() in D:\Halil\GitHub\module-zero\src\Abp.Zero\MultiTenancy\TenantCache.cs:line 38 at Abp.Runtime.Caching.TypedCacheExtensions.<>c__DisplayClass0_02.<Get>b__0(TKey k) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Caching\TypedCacheExtensions.cs:line 13 at Abp.Runtime.Caching.CacheExtensions.<>c__DisplayClass3_02.<Get>b__0(String k) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Caching\CacheExtensions.cs:line 28 at Abp.Runtime.Caching.CacheBase.Get(String key, Func2 factory) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Caching\CacheBase.cs:line 42 at Abp.Runtime.Caching.CacheExtensions.Get[TKey,TValue](ICache cache, TKey key, Func2 factory) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Caching\CacheExtensions.cs:line 28 at Abp.Runtime.Caching.TypedCacheWrapper2.Get(TKey key, Func2 factory) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Caching\TypedCacheWrapper.cs:line 52 at Abp.Runtime.Caching.TypedCacheExtensions.Get[TKey,TValue](ITypedCache2 cache, TKey key, Func1 factory) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Caching\TypedCacheExtensions.cs:line 13 at Abp.MultiTenancy.TenantCache2.Get(Int32 tenantId) in D:\Halil\GitHub\module-zero\src\Abp.Zero\MultiTenancy\TenantCache.cs:line 0 at Castle.Proxies.Invocations.TenantCache2_Get.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.TenantCache2Proxy.Get(Int32 tenantId) at Abp.Zero.EntityFramework.DbPerTenantConnectionStringResolver.GetNameOrConnectionString(DbPerTenantConnectionStringResolveArgs args) in D:\Halil\GitHub\module-zero\src\Abp.Zero.EntityFramework\Zero\EntityFramework\DbPerTenantConnectionStringResolver.cs:line 57 at Abp.Zero.EntityFramework.DbPerTenantConnectionStringResolver.GetNameOrConnectionString(ConnectionStringResolveArgs args) in D:\Halil\GitHub\module-zero\src\Abp.Zero.EntityFramework\Zero\EntityFramework\DbPerTenantConnectionStringResolver.cs:line 46 at Abp.Domain.Uow.UnitOfWorkBase.ResolveConnectionString(ConnectionStringResolveArgs args) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkBase.cs:line 332 at Abp.EntityFramework.Uow.EfUnitOfWork.GetOrCreateDbContext[TDbContext](Nullable1 multiTenancySide) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Uow\EfUnitOfWork.cs:line 145 at Abp.EntityFramework.Uow.UnitOfWorkExtensions.GetDbContext[TDbContext](IActiveUnitOfWork unitOfWork, Nullable1 multiTenancySide) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Uow\UnitOfWorkExtensions.cs:line 38 at Abp.EntityFramework.Uow.UnitOfWorkDbContextProvider1.GetDbContext(Nullable1 multiTenancySide) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Uow\UnitOfWorkDbContextProvider.cs:line 38 at Castle.Proxies.EfRepositoryBase2Proxy_3.get_Context_callback() at Castle.Proxies.Invocations.EfRepositoryBase3_get_Context_22.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 54 at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 47 at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.EfRepositoryBase2Proxy_3.get_Context() at Abp.EntityFramework.Repositories.EfRepositoryBase3.get_Table() in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Repositories\EfRepositoryBaseOfTEntityAndTPrimaryKey.cs:line 30 at Castle.Proxies.Invocations.EfRepositoryBase3_get_Table_22.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 54 at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 47 at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.EfRepositoryBase2Proxy_3.get_Table() at Castle.Proxies.EfRepositoryBase2Proxy_3.GetAll_callback() at Castle.Proxies.Invocations.EfRepositoryBase3_GetAll_22.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 54 at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 47 at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.EfRepositoryBase2Proxy_3.GetAll() at Abp.Authorization.Roles.AbpRoleStore2.get_Roles() in D:\Halil\GitHub\module-zero\src\Abp.Zero\Authorization\Roles\AbpRoleStore.cs:line 42 at Microsoft.AspNet.Identity.RoleManager2.get_Roles() at Castle.Proxies.Invocations.RoleManager2_get_Roles.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.RoleManagerProxy.get_Roles() at Portal.Web.Areas.Tools.Controllers.UsersController.<CreateUser>d__17.MoveNext() in C:\Users\admin.tom.adams.CTL-PORTAL\Documents\Visual Studio 2015\Projects\Portal\Portal.Web\Areas\Tools\Controllers\UsersController.cs:line 301 InnerException:

Any ideas what could be causing this? Thanks


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You might have two Id fields in one of your entities.

    If it is not the case, can you share a bit more part of your controller code. And which versions of ABP do you use ?

  • User Avatar
    0
    tjackadams created

    @ismcagdas What would define an Id field? If it is just a field named "Id" then i can't think of any that would have duplicates.

    Here is a bit of the controller code

    [HttpPost]
            [UnitOfWork]
            public virtual async Task<ActionResult> CreateUser(CreateUserViewModel model)
            {
                try
                {
                    CheckModelState();
    
                    // Validation code removed 
    
                    // Switch to the tenant
                    if (tenant != null)
                    {
                        _unitOfWorkManager.Current.EnableFilter(AbpDataFilters.MayHaveTenant);
                        _unitOfWorkManager.Current.SetTenantId(tenant.Id);
                    }
    
                    // Add default roles
                    user.Roles = new List<UserRole>();
                    var roles = await _roleManager.Roles.Where(r => r.IsDefault).ToListAsync();
    
                    if (roles.Any())
                    {
                        foreach (var defaultRole in roles)
                        {
                            user.Roles.Add(new UserRole { RoleId = defaultRole.Id });
                        }
                    }                
    
                    // Save User
                    CheckErrors(await _userManager.CreateAsync(user));
                    await _unitOfWorkManager.Current.SaveChangesAsync();
    
    
                }
                catch (UserFriendlyException ex)
                {
                    ViewBag.ErrorMessage = ex.Message;
    
                    return View("CreateUser", model);
                }
            }
    

    I've removed some validation code at the top as this works. Just on another note as well, if i comment out the code that throws the exception, then another slice of code also throws the same exception

    CheckErrors(await _userManager.CreateAsync(user));
    

    Thanks