title: ASP.NET - C# Reflection: AddObject results in "Ambiguous match found exception" during Runtime
body: <p>I am developing the Save Profile section of a web application (in ASP.NET/C#), however, I am getting an "Ambiguous match found" exception whenever the code tries to save a new profile through invoking each of the "AddObject", during runtime. The code is as follows partly:  </p>
  
  <pre><code>using System;
  using System.ComponentModel.Composition;
  using System.Linq;
  using Itok.DataAccess.Interfaces;
  using System.Collections.Generic;
  using Itok.Entities;
  
  namespace Itok.DataAccess.Repositories
  {
      [Export(typeof(IProfileRepository))] 
      public class ProfileRepository : IProfileRepository
      {
          private Connection conn;
          public Int32 SaveProfile(Profile profile)
          {
              Int32 profileID1 = 0;
              Boolean HasAttributeId = false;
              profile.LastUpdateDate = DateTime.Now;
              using(ItokDataContext dc = conn.GetContext())
              {
                  if (profile.ProfileID &gt; 0)
                 {
                      dc.Profiles.Attach(new Profile { ProfileID = profile.ProfileID }); 
                      dc.Profiles.ApplyCurrentValues(profile);
                      if (profile.Attributes != null)
                      {
                          foreach(ProfileAttribute item in profile.Attributes)
                          {
                              dc.ProfileAttributes.Attach(new ProfileAttribute
                                                          { ProfileAttributeID = item.ProfileAttributeID,
                                                         });
                              dc.ProfileAttributes.ApplyCurrentValues(item);
                          }
                      }
                  }
                  else
                  {
                      profile.CreateDate = DateTime.Now;
                      //when working with new profile we don't expect attributes to be null, but check anyway
                      if (profile.Attributes != null)
                      {
                          foreach (ProfileAttribute item in profile.Attributes)
                          {                            
                              dc.ProfileAttributes.AddObject(new ProfileAttribute
                              {
                                  ProfileAttributeID = item.ProfileAttributeID,
                                  ProfileAttributeTypeID = item.ProfileAttributeTypeID,
                                  ProfileID = item.ProfileID,
                                  Response = item.Response,
                                  CreateDate = item.CreateDate,
                                  TimeStamp = item.TimeStamp
                              });
                              HasAttributeId = true;
                          }
                      }
                      dc.Profiles.AddObject(profile);
                  }
                  dc.SaveChanges();
                  profileID1 = profile.ProfileID;
                  if (HasAttributeId)
                  {
                      var result = from pa in dc.ProfileAttributes.Include("ProfileAttributeType")
                                   where pa.ProfileID == profileID1
                                   select pa;
                      profile.Attributes = result.ToList();
                      foreach (ProfileAttribute item in profile.ProfileAttributes)
                      {
                          item.profileAttributeType = item.ProfileAttributeType;
                      }
                  }
              }
              return profileID1;
          }
      }
  }
  </code></pre>
  
  <p>The ItokModel.context.cs includes the following code: </p>
  
  <pre><code>namespace Itok.Entities
  {
      public partial class ItokDataContext : ObjectContext
      {
          public const string ConnectionString = "name=ItokDataContext";
          public const string ContainerName = "ItokDataContext";
          #region Constructors
          public ObjectSet&lt;ProfileAttribute&gt; ProfileAttributes
          {
              get { return _profileAttributes  ?? (_profileAttributes = CreateObjectSet&lt;ProfileAttribute&gt;("ProfileAttributes")); }
          }
          private ObjectSet&lt;ProfileAttribute&gt; _profileAttributes;
      } 
  }
  </code></pre>
  
  <p>The Profile.cs includes the following code: </p>
  
  <pre><code>namespace Itok.Entities
  {
      public partial class Profile
      {
          public List&lt;ProfileAttribute&gt; Attributes { get; set; }
          public LevelOfExperienceType levelOfExperienceType { get; set; }
          public Profile()
          {
              Attributes = new List&lt;ProfileAttribute&gt;();
          }
      }
  }
  </code></pre>
  
  <p>And here is the StackTrace:</p>
  
  <pre><code>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.Objects.Internal.EntityProxyFactory.CreateBaseGetter(Type declaringType, PropertyInfo propertyInfo)
  at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.GetNavigationPropertyValue(RelatedEnd relatedEnd)
  at System.Data.Objects.Internal.EntityWrapper`1.GetNavigationPropertyValue(RelatedEnd relatedEnd)
  at System.Data.Objects.EntityEntry.FixupFKValuesFromNonAddedReferences()
  at System.Data.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName)
  at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
  at System.Data.Objects.ObjectSet`1.AddObject(TEntity entity)
  at Itok.DataAccess.Repositories.ProfileRepository.SaveProfile(Profile profile) in C:\Users\S400\Documents\Visual Studio 2010\Projects\Itok\Itok\DataAccess\Repositories\ProfileRepository.cs:line 84
  </code></pre>
  
  <p>Does anybody have an idea how to avoid this exception??</p>