title: System.ArgumentException: Duplicate type name within an assembly
body: <p>I am working on an ASP.Net MVC 3 web application using EF 4.1. Since today, I am getting this error:</p>
  
  <p>System.ArgumentException: Duplicate type name within an assembly</p>
  
  <p>and I can't figure out what is causing it. It is happening in my repository when performing a Find:</p>
  
  <pre><code>public virtual T GetById(long id)
  {
      return dbset.Find(id);
  }
  </code></pre>
  
  <p>Here is the stack trace:</p>
  
  <pre><code>[ArgumentException: Duplicate type name within an assembly.]
     System.Reflection.Emit.ModuleBuilder.CheckTypeNameConflict(String strTypeName, TypeBuilder enclosingType) +9544354
     System.Reflection.Emit.AssemblyBuilderData.CheckTypeNameConflict(String strTypeName, TypeBuilder enclosingType) +53
     System.Reflection.Emit.TypeBuilder.Init(String fullname, TypeAttributes attr, Type parent, Type[] interfaces, Module module, PackingSize iPackingSize, Int32 iTypeSize, TypeBuilder enclosingType) +178
     System.Reflection.Emit.ModuleBuilder.DefineType(String name, TypeAttributes attr, Type parent, Type[] interfaces) +118
     System.Data.Objects.Internal.ProxyTypeBuilder.get_TypeBuilder() +299
     System.Data.Objects.Internal.ProxyTypeBuilder.CreateType(ModuleBuilder moduleBuilder) +227
     System.Data.Objects.Internal.EntityProxyFactory.BuildType(ModuleBuilder moduleBuilder, ClrEntityType ospaceEntityType) +63
     System.Data.Objects.Internal.EntityProxyFactory.TryCreateProxyType(EntityType ospaceEntityType) +193
     System.Data.Objects.Internal.EntityProxyFactory.GetProxyType(ClrEntityType ospaceEntityType) +104
     System.Data.Common.Internal.Materialization.Translator.Visit(EntityColumnMap columnMap, TranslatorArg arg) +178
     System.Data.Query.InternalTrees.EntityColumnMap.Accept(ColumnMapVisitorWithResults`2 visitor, TArgType arg) +16
     System.Data.Common.Internal.Materialization.Translator.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue) +116
     System.Data.Common.Internal.Materialization.Translator.Visit(SimpleCollectionColumnMap columnMap, TranslatorArg arg) +13
     System.Data.Query.InternalTrees.SimpleCollectionColumnMap.Accept(ColumnMapVisitorWithResults`2 visitor, TArgType arg) +17
     System.Data.Common.Internal.Materialization.Translator.TranslateColumnMap(QueryCacheManager queryCacheManager, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean valueLayer) +314
     System.Data.Common.Internal.Materialization.TypedShaperFactoryCreator`1.TypedCreate(QueryCacheManager cacheManager, ColumnMap columnMap, MetadataWorkspace metadata, SpanIndex spanInfo, MergeOption mergeOption, Boolean valueLayer) +64
     System.Data.Common.Internal.Materialization.ShaperFactory.Create(Type elementType, QueryCacheManager cacheManager, ColumnMap columnMap, MetadataWorkspace metadata, SpanIndex spanInfo, MergeOption mergeOption, Boolean valueLayer) +185
     System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Span span, ReadOnlyCollection`1 compiledQueryParameters) +285
     System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +696
     System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +149
     System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator() +44
     System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +184
     System.Data.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__2(IEnumerable`1 sequence) +41
     System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +59
     System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +150
     System.Linq.Queryable.SingleOrDefault(IQueryable`1 source) +265
     System.Data.Entity.Internal.Linq.InternalSet`1.FindInStore(WrappedEntityKey key, String keyValuesParamName) +602
     System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues) +124
     System.Data.Entity.DbSet`1.Find(Object[] keyValues) +12
     XXXXXXXX.Data.Infrastructure.RepositoryBase`1.GetById(Int64 id) in C:\XXXXXXXX\XXXXXXXX\XXXXXXXX.Data\Infrastructure\RepositoryBase.cs:58
     MatchesHorsConcours.Service.MatchService.GetMatch(Int32 id) in C:\XXXXXXXX\XXXXXXXX\XXXXXXXX.Service\XxxService.cs:55
     XXXXXXXX.Controllers.XXXXXXXX.Edit(Int32 id) in C:\XXXXXXXX\XXXXXXXX\XXXXXXXX.Web\Controllers\XxxController.cs:130
     lambda_method(Closure , ControllerBase , Object[] ) +101
     System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
     System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
     System.Web.Mvc.&lt;&gt;c__DisplayClass15.&lt;InvokeActionMethodWithFilters&gt;b__12() +55
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
     System.Web.Mvc.&lt;&gt;c__DisplayClass17.&lt;InvokeActionMethodWithFilters&gt;b__14() +19
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
     System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
     System.Web.Mvc.Controller.ExecuteCore() +116
     System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
     System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
     System.Web.Mvc.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() +37
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +21
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +12
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
     System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +50
     System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f) +7
     System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920029
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +184
  </code></pre>
  
  <p>This could be related to table in which I duplicated a foreign key. It was called something like myKeyId, and I now have myKey1Id and myKey2Id in that table. I checked everywhere in my code, and I can't find myKeyId anymore.</p>
  
  <p>The error message is so generic that I don't know how to pin-point what is causing this error. Any advice would be welcome.</p>