title: The type has already been configured as an entity type. It cannot be reconfigured as a complex type
body: <p>I don't know why but I get this error at <code>ctx.Database.Initialize(false)</code></p>
  
  <p>Exception details (bij = at)</p>
  
  <pre><code>System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=The type 'Contact' has already been configured as an entity type. It cannot be reconfigured as a complex type.
  Source=EntityFramework
  StackTrace:
     bij System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ComplexType(Type complexType)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.&lt;&gt;c__DisplayClass9.&lt;MapComplexType&gt;b__6()
     bij System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.&lt;&gt;c__DisplayClass2.&lt;MapPrimitiveOrComplexOrEnumProperty&gt;b__0()
     bij System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.PropertyConfigurationConventionDispatcher.&lt;Dispatch&gt;b__f[TPropertyConfiguration]()
     bij System.Data.Entity.ModelConfiguration.Configuration.ConventionPrimitivePropertyConfiguration.IsRequired()
     bij System.Data.Entity.ModelConfiguration.Conventions.RequiredPrimitivePropertyAttributeConvention.Apply(ConventionPrimitivePropertyConfiguration configuration, RequiredAttribute attribute)
     bij System.Data.Entity.ModelConfiguration.Conventions.PrimitivePropertyAttributeConfigurationConvention`1.&lt;.ctor&gt;b__0(ConventionPrimitivePropertyConfiguration configuration, IEnumerable`1 attributes)
     bij System.Data.Entity.ModelConfiguration.Conventions.PropertyConventionWithHaving`1.ApplyCore(PropertyInfo memberInfo, Func`1 configuration, ModelConfiguration modelConfiguration)
     bij System.Data.Entity.ModelConfiguration.Conventions.PropertyConventionBase.Apply(PropertyInfo memberInfo, Func`1 configuration, ModelConfiguration modelConfiguration)
     bij System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.PropertyConfigurationConventionDispatcher.Dispatch[TPropertyConfiguration]()
     bij System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.PropertyConfigurationConventionDispatcher.Dispatch()
     bij System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyPropertyConfiguration(PropertyInfo propertyInfo, Func`1 propertyConfiguration, ModelConfiguration modelConfiguration)
     bij System.Data.Entity.ModelConfiguration.Conventions.Convention.ApplyPropertyConfiguration(PropertyInfo propertyInfo, Func`1 propertyConfiguration, ModelConfiguration modelConfiguration)
     bij System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyPropertyConfiguration(PropertyInfo propertyInfo, Func`1 propertyConfiguration, ModelConfiguration modelConfiguration)
     bij System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.MapPrimitiveOrComplexOrEnumProperty(PropertyInfo propertyInfo, Func`1 structuralTypeConfiguration, Boolean discoverComplexTypes)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.&lt;&gt;c__DisplayClassb.&lt;MapComplexType&gt;b__7(PropertyMapper m, PropertyInfo p)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapStructuralElements[TStructuralTypeConfiguration](Type type, ICollection`1 annotations, Action`2 propertyMappingAction, Func`1 structuralTypeConfiguration)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapComplexType(Type type, Boolean discoverNested)
     bij System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.MapPrimitiveOrComplexOrEnumProperty(PropertyInfo propertyInfo, Func`1 structuralTypeConfiguration, Boolean discoverComplexTypes)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.&lt;&gt;c__DisplayClassb.&lt;MapComplexType&gt;b__7(PropertyMapper m, PropertyInfo p)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapStructuralElements[TStructuralTypeConfiguration](Type type, ICollection`1 annotations, Action`2 propertyMappingAction, Func`1 structuralTypeConfiguration)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapComplexType(Type type, Boolean discoverNested)
     bij System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.MapPrimitiveOrComplexOrEnumProperty(PropertyInfo propertyInfo, Func`1 structuralTypeConfiguration, Boolean discoverComplexTypes)
     bij System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.MapIfNotNavigationProperty(PropertyInfo propertyInfo, EntityType entityType, Func`1 entityTypeConfiguration)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.&lt;&gt;c__DisplayClass14.&lt;MapEntityType&gt;b__e(PropertyMapper m, PropertyInfo p)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapStructuralElements[TStructuralTypeConfiguration](Type type, ICollection`1 annotations, Action`2 propertyMappingAction, Func`1 structuralTypeConfiguration)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
     bij System.Data.Entity.ModelConfiguration.Mappers.NavigationPropertyMapper.Map(PropertyInfo propertyInfo, EntityType entityType, Func`1 entityTypeConfiguration)
     bij System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
     bij System.Data.Entity.DbModelBuilder.MapTypes(EdmModel model)
     bij System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
     bij System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
     bij System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
     bij System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
     bij System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
     bij System.Data.Entity.Database.Initialize(Boolean force)
     bij Contacts.DAL.EF.EFRepository..ctor() in C:\VisualStudio\p1w1\Prak_Contacten\DAL\EF\EFRepository.cs:regel 17
     bij Contacts.BL.ContactManager..ctor() in C:\VisualStudio\p1w1\Prak_Contacten\BL\ContactManager.cs:regel 18
     bij UI.CA.Program.Main(String[] args) in C:\VisualStudio\p1w1\Prak_Contacten\UI.CA\Program.cs:regel 18
     bij System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
     bij System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
     bij Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     bij System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     bij System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     bij System.Threading.ThreadHelper.ThreadStart()
     InnerException: 
  </code></pre>
  
  <p>Here is my code
  Context</p>
  
  <pre><code>namespace Contacts.DAL.EF
  {
  
  [DbConfigurationType(typeof(ContactsDbConfiguration))]
  internal class ContactsDbContext : DbContext
  {
      public ContactsDbContext() : base("ContactsDBEF")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
          modelBuilder.Entity&lt;Contact&gt;().Property(c =&gt; c.Name).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute()));
          modelBuilder.Entity&lt;Contact&gt;().HasKey(c =&gt; c.PersonId);
          modelBuilder.ComplexType&lt;Address&gt;();
          modelBuilder.ComplexType&lt;Address&gt;().Ignore(a =&gt; a.AddressId);
          modelBuilder.Entity&lt;Category&gt;().HasKey(c =&gt; c.CatergoryId);
       }
  
  
      public DbSet&lt;Contact&gt; Contacts { get; set; }
      public DbSet&lt;Category&gt; Categories { get; set; }
   }
  }
  </code></pre>
  
  <p>Configuration   </p>
  
  <pre><code> namespace Contacts.DAL.EF
   {
   class ContactsDbConfiguration:DbConfiguration
   {
      public ContactsDbConfiguration()
      {
  
          this.SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
          this.SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
  
          this.SetDatabaseInitializer&lt;ContactsDbContext&gt;(new ContactsDbInitializer());
      }
  }
  }
  </code></pre>
  
  <p>Initializer</p>
  
  <pre><code> namespace Contacts.DAL.EF
   {
    internal class ContactsDbInitializer:DropCreateDatabaseAlways&lt;ContactsDbContext&gt;
  {
      protected override void Seed(ContactsDbContext context)
      {
          context.Categories.Add(new Category()
          {
              CatergoryId = 1,
              Description = "Family"
          });
          context.Categories.Add(new Category()
          {
              CatergoryId = 2,
              Description = "Friends"
          });
          context.Categories.Add(new Category()
          {
              CatergoryId = 3,
              Description = "School"
          });
          context.Categories.Add(new Category()
          {
              CatergoryId = 4,
              Description = "Sports"
          });
          context.SaveChanges();
  
          context.Contacts.Add(new Contact()
          {
              PersonId = 1,
              Name = "Verstraeten Micheline",
              Address = new Address()
              {
                  StreetAndNumber = "Antwerpsestraat 20",
                  Zipcode = 2000,
                  City = "Antwerpen"
              },
              Gender = Gender.Female,
              Blocked = false,
              Birthday = new DateTime(1978, 8, 30),
              Phone = "03/123.45.67",
              Mobile = "0495/11.22.33",
              Categories = new List&lt;Category&gt;() { context.Categories.ToList()[0] }
          });
          context.Contacts.Add(new Contact()
          {
              PersonId = 2,
              Name = "Bogaerts Sven",
              Address = new Address()
              {
                  StreetAndNumber = "Brusselsestraat 10",
                  Zipcode = 1000,
                  City = "Brussel"
              },
              Gender = Gender.Male,
              Blocked = false,
              Birthday = new DateTime(1975, 4, 12),
              Mobile = "0478/12.34.56",
              Categories = new List&lt;Category&gt;() { context.Categories.ToList()[0], context.Categories.ToList()[2], context.Categories.ToList()[3] }
          });
          context.Contacts.Add(new Contact()
          {
              PersonId = 3,
              Name = "Vlaeminchx Dieter",
              Address = new Address()
              {
                  StreetAndNumber = "Gentsestraat 95",
                  Zipcode = 9000,
                  City = "Gent"
              },
              Gender = Gender.Male,
              Blocked = true,
              Birthday = new DateTime(1981, 12, 8),
              Mobile = "0479 / 98.76.54",
              Categories = new List&lt;Category&gt;() { context.Categories.ToList()[0], context.Categories.ToList()[3] }
          });
          context.SaveChanges();
      }
  
  }
  }
  </code></pre>
  
  <p>Repository with data methods</p>
  
  <pre><code>namespace Contacts.DAL.EF
  {
  public class EFRepository : IRepository
  {
      private ContactsDbContext ctx;
  
      public EFRepository()
      {
          ctx = new ContactsDbContext();
          ctx.Database.Initialize(false);
      }
  
      public void CreateContact(Contact contactToInsert)
      {
          ctx.Contacts.Add(contactToInsert);
          ctx.SaveChanges();
      }
  
      public void DeleteContact(int id)
      {
          IEnumerable&lt;Contact&gt; temp = ctx.Contacts.AsEnumerable();
          Contact tempc = null;
          foreach(Contact c in temp)
          {
              if (c.PersonId==id)
              {
                  tempc = c;
              }
          }
          ctx.Contacts.Remove(tempc);
          ctx.SaveChanges();
      }
  
      public IEnumerable&lt;Category&gt; ReadAllCategories()
      {
          return ctx.Categories.AsEnumerable();
      }
  
      public IEnumerable&lt;Contact&gt; ReadAllContacts(int? categoryId = default(int?))
      {
          if (categoryId == null)
          {
              return ctx.Contacts.AsEnumerable();
          }
          else {
              IEnumerable&lt;Contact&gt; temp = new List&lt;Contact&gt;();
              foreach (Contact c in ctx.Contacts.AsEnumerable()){
                  foreach (Category ca in c.Categories)
                  {
                      if (ca.CatergoryId == categoryId)
                      {
                          temp.ToList().Add(c);
                      }
                  }
              }
              return temp;
          }                     
      }
  
      public Contact ReadContact(int id)
      {
          Contact temp=new Contact();
          foreach(Contact c in ctx.Contacts.AsEnumerable())
          {
              if(c.PersonId == id)
              {
                  temp = c;
              }              
          }
          return temp;
      }
  
      public void UpdateContact(Contact contactToUpdate, Contact c)
      {
          for(int i = 0; i &lt; ctx.Contacts.Count(); i++)
          {
              if (ctx.Contacts.ElementAt(i).PersonId == contactToUpdate.PersonId)
              {
                  Contact temp = ctx.Contacts.ElementAt(i);
                  temp = c;
                  ctx.SaveChanges();
              }
          }
      }
  }
  }
  </code></pre>