title: Code First The relationship was not loaded because the type is not available using fluent api
body: <p>I'm trying to get a code first project to create my database; I've gotten all of the schema errors resolved but now I'm receiving the following error.</p>
  
  <p>The relationship 'EquipmentPro.Entities.Correspondence_UserFrom' was not loaded because the type 'EquipmentPro.Entities.AspNetUsers' is not available.</p>
  
  <p>I've read some of the posts here and most of them say that the problem is because the 'AspNetUsers' class is not in the same namespace as the class the relationship is on. That was true at first so I moved AspNetUsers into the same folder as the other model object and fixed the namespace but I'm still getting the error. I'm using fluent API to create the tables and relationships.</p>
  
  <p>Here is the Correspondence model:</p>
  
  <pre><code>public class Correspondence : EntityBase
  {
      public Correspondence()
      {
          ChildCorrespondences = new HashSet&lt;Correspondence&gt;();
      }
  
      public int CorrespondenceId { get; set; }
      public int CorrespondenceTypeId { get; set; }
      public int? ParentCorrespondenceId { get; set; }
      public string UserIdFrom { get; set; }
      public string UserIdTo { get; set; }
      public int LanguageId { get; set; }
      public int? EquipmentItemId { get; set; }
      public int? CartId { get; set; }
      public string MessageText { get; set; }
      public bool IsFlaggedForReview { get; set; }
  
      public Correspondence ParentCorrespondence { get; set; }
      public CorrespondenceType CorrespondenceType { get; set; }
      public AspNetUsers UserFrom { get; set; }
      public AspNetUsers UserTo { get; set; }
      public Language Language { get; set; }
      public EquipmentItem EquipmentItem { get; set; }
      public Cart Cart { get; set; }
  
      public ICollection&lt;Correspondence&gt; ChildCorrespondences { get; set; }
  }
  </code></pre>
  
  <p>The AspNetUsers model is the standard .Net membership users model and here is the fluent API code:</p>
  
  <pre><code>public CorrespondenceMapping()
  {
      HasKey(t =&gt; t.CorrespondenceId);
      ToTable("Correspondence");
  
      Property(t =&gt; t.CorrespondenceId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      Property(t =&gt; t.CorrespondenceTypeId).IsRequired();
      Property(t =&gt; t.ParentCorrespondenceId);
      Property(t =&gt; t.UserIdFrom).HasMaxLength(128).IsRequired().HasColumnType("nvarchar");
      Property(t =&gt; t.UserIdTo).HasMaxLength(128).HasColumnType("nvarchar");
      Property(t =&gt; t.LanguageId).IsRequired();
      Property(t =&gt; t.EquipmentItemId);
      Property(t =&gt; t.CartId);
      Property(t =&gt; t.MessageText).IsRequired().HasColumnType("nvarchar");
      Property(t =&gt; t.IsFlaggedForReview).IsRequired();
  
      HasOptional(t =&gt; t.ParentCorrespondence)
          .WithMany(t =&gt; t.ChildCorrespondences)
          .HasForeignKey(t =&gt; t.ParentCorrespondenceId)
          .WillCascadeOnDelete();
  
      HasRequired(t =&gt; t.CorrespondenceType)
          .WithMany(t =&gt; t.Correspondences)
          .HasForeignKey(t =&gt; t.CorrespondenceTypeId)
          .WillCascadeOnDelete();
  
      // the error is with this relaionship
      HasRequired(t =&gt; t.UserFrom).WithMany(t =&gt; t.Correspondences).HasForeignKey(t =&gt; t.UserIdFrom);
      HasOptional(t =&gt; t.UserTo).WithMany(t =&gt; t.Correspondences).HasForeignKey(t =&gt; t.UserIdTo);
      HasRequired(t =&gt; t.Language).WithMany(t =&gt; t.Correspondences).HasForeignKey(t =&gt; t.LanguageId);
      HasOptional(t =&gt; t.EquipmentItem).WithMany(t =&gt; t.Correspondences).HasForeignKey(t =&gt; t.EquipmentItemId);
      HasOptional(t =&gt; t.Cart).WithMany(t =&gt; t.Correspondences).HasForeignKey(t =&gt; t.CartId);
  }
  </code></pre>
  
  <p>Here is the DbContext class:</p>
  
  <pre><code>public class EquipmentDataDbContext : DbContext
  {
      public EquipmentDataDbContext()
          : base("name=EquipmentData")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
  
          // adding the equipment data model mappings
          modelBuilder.Configurations.Add(new CartMapping());
          modelBuilder.Configurations.Add(new CartItemMapping());
          modelBuilder.Configurations.Add(new CartStatusTypeMapping());
          modelBuilder.Configurations.Add(new CorrespondenceMapping());
          modelBuilder.Configurations.Add(new CorrespondenceTypeMapping());
          modelBuilder.Configurations.Add(new CurrencyTypeMapping());
          modelBuilder.Configurations.Add(new EquipmentAuctionMapping());
          modelBuilder.Configurations.Add(new EquipmentAuctionBidMapping());
          modelBuilder.Configurations.Add(new EquipmentBidMapping());
          modelBuilder.Configurations.Add(new EquipmentInventoryMapping());
          modelBuilder.Configurations.Add(new EquipmentItemMapping());
          modelBuilder.Configurations.Add(new EquipmentItemPropertyDefaultMapping());
          modelBuilder.Configurations.Add(new EquipmentItemPropertyValueMapping());
          modelBuilder.Configurations.Add(new EquipmentItemTypeMapping());
          modelBuilder.Configurations.Add(new EquipmentPropertyMapping());
          modelBuilder.Configurations.Add(new EquipmentPropertyDisplayTypeMapping());
          modelBuilder.Configurations.Add(new EquipmentPropertyOptionMapping());
          modelBuilder.Configurations.Add(new EquipmentPropertyValueTypeMapping());
          modelBuilder.Configurations.Add(new MediaMapping());
          modelBuilder.Configurations.Add(new MediaTypeMapping());
          modelBuilder.Configurations.Add(new TagMapping());
  
          // adding the membership data model mappings
          modelBuilder.Configurations.Add(new AspNetRolesMapping());
          modelBuilder.Configurations.Add(new AspNetUserClaimsMapping());
          modelBuilder.Configurations.Add(new AspNetUserLoginsMapping());
          modelBuilder.Configurations.Add(new AspNetUsersMapping());
      }
  
      // Equipment Data Models
      public DbSet&lt;Cart&gt; Carts { get; set; }
      public DbSet&lt;CartItem&gt; CartItems { get; set; }
      public DbSet&lt;CartStatusType&gt; CartStatuses { get; set; }
      public DbSet&lt;Correspondence&gt; Correspondences { get; set; }
      public DbSet&lt;CorrespondenceType&gt; CorrespondenceTypes { get; set; }
      public DbSet&lt;CurrencyType&gt; CurrencyTypes { get; set; }
      public DbSet&lt;EquipmentAuction&gt; EquipmentAuctions { get; set; }
      public DbSet&lt;EquipmentAuctionBid&gt; EquipmentAuctionBids { get; set; }
      public DbSet&lt;EquipmentBid&gt; EquipmentBids { get; set; }
      public DbSet&lt;EquipmentInventory&gt; EquipmentInventory { get; set; }
      public DbSet&lt;EquipmentItem&gt; EquipmentItems { get; set; }
      public DbSet&lt;EquipmentItemPropertyDefault&gt; EquipmentItemPropertyDefault { get; set; }
      public DbSet&lt;EquipmentItemPropertyValue&gt; EquipmentItemPropertyValues { get; set; }
      public DbSet&lt;EquipmentItemType&gt; EquipmentItemTypes { get; set; }
      public DbSet&lt;EquipmentProperty&gt; EquipmentProperties { get; set; }
      public DbSet&lt;EquipmentPropertyDisplayType&gt; EquipmentPropertyDisplayTypes { get; set; }
      public DbSet&lt;EquipmentPropertyOption&gt; EquipmentPropertyOptions { get; set; }
      public DbSet&lt;EquipmentPropertyValueType&gt; EquipmentPropertyValueTypes { get; set; }
      public DbSet&lt;Media&gt; Media { get; set; }
      public DbSet&lt;MediaType&gt; MediaTypes { get; set; }
      public DbSet&lt;Tag&gt; Tags { get; set; }
  
      // Membership Models
      public DbSet&lt;AspNetRoles&gt; AspNetRoles { get; set; }
      public DbSet&lt;AspNetUserClaims&gt; AspNetUserClaims { get; set; }
      public DbSet&lt;AspNetUserLogins&gt; AspNetUserLogins { get; set; }
      public DbSet&lt;AspNetUsers&gt; AspNetUsers { get; set; }
  }
  </code></pre>
  
  <p>I've split my solution into 3 different projects a web project, a project with just the entity models and a project with the mappings and the database context in it. I'm running the initial create from the web project when I get this error.</p>
  
  <p>Does anyone have an idea why I would be getting this error?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I decided to remove the relationship throwing the error and it appeared that everything was successful but no tables have been created in the database. The target database listed in the message is the correct one but no tables were created.</p>