无法在实体类型上设置字段/属性

时间:2015-10-20 10:35:36

标签: asp.net-mvc entity-framework asp.net-mvc-4

我有两个模型如下

public class np_claim_hdr
    {
        [Key]
        public decimal CLAIM_ID { get; set; }
        ----------
         ---------------
        --------------
     [InverseProperty("header")]
        public virtual IList<np_claim_dtls> np_claim_dtls { get; set; }
}


public class np_claim_dtls
    {
         [Key]

        public decimal CLAIM_ID { get; set; }


        [ForeignKey("CLAIM_ID"), InverseProperty("np_claim_dtls")]
        public virtual np_claim_hdr header { get; set; }
        public string PROV_CODE { get; set; }
        public string PROV_DESC { get; set; }
        public string PRIMARY { get; set; }
}

当我设置模型np_claim_hdr的对象时,它给出了与np_claim_dtls

行相关的错误

我试过如下

 np_claim_hdr header = new np_claim_hdr();
 header = db.np_claim_hdr.Find(1500);

并且它的值为np_claim_hdrnp_claim_dtls的值在同一个内部会出现错误,如下面的屏幕截图所示

enter image description here

  

类型&#39; System.Data.Entity.Core.EntityException&#39;的例外情况   发生在EntityFramework.dll中但未在用户代码中处理

     

其他信息:无法设置字段/属性np_claim_dtls   实体类型   System.Data.Entity.DynamicProxies.np_claim_hdr_047D7A4CFCF9316F6A7AEE7D891D9077FC5B931247DC389C8EB4D53A2F935577。有关详细信息,请参阅InnerException。

     

InnerException:确保源类型可转换为   目的地类型。

     

InnerException:从数字转换时,该值必须是数字   少于无限

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。发布答案是因为它可能在将来帮助其他人。

在两个模型中都使用CLAIM_ID作为[Key],因为CLAIM_IDnp_claim_hdr的主键在np_claim_dtls中用作外键,因此无法使用np_claims_dtls。因此,为模型public class np_claim_hdr { [Key] public decimal CLAIM_ID { get; set; } ---------- --------------- -------------- [InverseProperty("header")] public virtual IList<np_claim_dtls> np_claim_dtls { get; set; } } public class np_claim_dtls { [Key] public decimal ID { get; set; } public decimal CLAIM_ID { get; set; } [ForeignKey("CLAIM_ID"), InverseProperty("np_claim_dtls")] public virtual np_claim_hdr header { get; set; } public string PROV_CODE { get; set; } public string PROV_DESC { get; set; } public string PRIMARY { get; set; } } 指定另一个主键解决了问题

<div class="navbar navbar-default top-bar">
  <div class="container">
    <div class="infos pull-right">

      <span class="lnr lnr-clock infos-icons"></span>
      <p class="infos-text">Mon-Fri: 08:00 - 12:00</p>
      <span class="divider-vertical"></span>

      <span class="lnr lnr-envelope infos-icons"></span>
      <p class="infos-text">mail.example.com</p>
      <span class="divider-vertical"></span>

      <span class="lnr lnr-phone infos-icons"></span>
      <p class="infos-text">(880)123-456-7890</p>

    </div>
  </div>
</div>