title: Entity Framework 6 - Navigation Properties PK and FK are different types
body: <p>I have an existing database that has a PK that is a numberic(18,0) and a FK that is an int.  When I try to use the navigation property EF throws an invalid cast exception.</p>
  
  <p>Is there a way to map this relationship that will sort out the invalid cast?</p>
  
  <p>In the code below promo_cfg.pc_id is numeric(18,0) and promo.pc_id is int.</p>
  
  <pre><code>public class PromotionMap : EntityTypeConfiguration&lt;Promotion&gt;
  {
      public PromotionMap()
      {
          // Primary Key
          this.HasKey(p =&gt; p.PromotionId);
  
          // Properties
  
          // table and column mappings
          this.ToTable("promo");
          this.Property(p =&gt; p.PromotionId).HasColumnName("p_id");
          this.Property(p =&gt; p.PromotionConfigId).HasColumnName("pc_id");
          this.Property(p =&gt; p.PromotionCode).HasColumnName("p_code");
  
  
          this.HasRequired(t =&gt; t.PromotionConfig) 
              .WithMany(t =&gt; t.Promotions)
              .HasForeignKey(d =&gt; new { d.PromotionConfigId }); 
      }
  }
  
  public class Promotion
  {
      public decimal PromotionId { get; set; }
      public int PromotionConfigId { get; set; }
      public string PromotionCode { get; set; }
  }
  
  public PromotionConfigMap()
  {
      // Primary Key
      this.HasKey(s =&gt; s.PromotionConfigId);
  
      // Properties
      // Table and Column mappings
      this.ToTable("promo_cfg");
  
      this.Property(p =&gt; p.PromotionConfigId).HasColumnName("pc_id");
      this.Property(p =&gt; p.AllowOrders).HasColumnName("allow_orders");
      this.HasOptional(p =&gt; p.Promotions).WithRequired().Map(x =&gt; x.MapKey("pc_id"));
  }
  
  public class PromotionConfig
  {
      public int PromotionConfigId { get; set; }
      public int AllowOrders { get; set; }
      public virtual ICollection&lt;Promotion&gt; Promotions { get; set; }
  }
  </code></pre>