2015-10-20 125 views
0

我有两个型号如下无法设置

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_dtlsnp_claim_hdr价值,但价值是给误差在屏幕截图所示下面

enter image description here

类型的“System.Data.Entity.Core.EntityException” 发生在EntityFramework.dll例外,但是在用户代码

附加没有处理信息:无法设置字段/属性np_claim_dtls 实体类型 System.Data.Entity.DynamicProxies.np_claim_hdr_047D7A4CFCF9316F6A7AEE7D891D9077FC5B931247DC389C8EB4D53A2F935577。详情请参阅InnerException。

的InnerException:确保源类型转换到 目标类型。

的InnerException:当从数铸造,值必须小于无穷

回答

0

一些 我发现这个问题。发布它的答案是因为它可能在未来帮助他人。

在这两个模型被用来作为CLAIM_ID[Key]因为np_claim_hdr主键的CLAIM_IDnp_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; } 
}