ASP MVC4值对于Int32来说太大或太小,但是对于字符串来说



这对于经验丰富的MVC(4)人员来说可能是显而易见的,所以请提前道歉。

我收到错误"值对于Int32来说太大或太小。"

Class LEARNER具有Key LearnRefNumber,该Key LearnRefNumber被定义为Class中的字符串。在我的模型中,它被一致地定义为字符串(CSDL)和varchar2(SSDL);这两者的XML片段都在本文的底部。

当我传递一个字符串时,属性被定义为字符串。我有点不明白为什么我会犯这个错误。它是MVC反射的结果吗?有人能提出一个变通办法吗?

该错误似乎发生在DbSet的Find方法中。

    // My Controller ActionResult Code
    public ActionResult Details(string id)
    {
        LEARNER learner = db.LEARNERS.Find(id);
        // The above line generates an exception when I pass in the string 20044010
        // as id.
        if (learner == null)
        {
            return HttpNotFound();
        }
        return View(learner);
    }

CSDL和SSDL的XML片段分别如下:

    <EntityType Name="LEARNER">
      <Key>
        <!--<PropertyRef Name="UKPRN" />-->
        <PropertyRef Name="LearnRefNumber" />
      </Key>
      <Property Type="String" Name="LearnRefNumber" Nullable="false" Unicode="false" FixedLength="false" MaxLength="12" />
      <Property Type="Int32" Name="ULN" Nullable="false" />
      <Property Type="String" Name="FamilyName" Nullable="false" Unicode="false" />
      <Property Type="String" Name="GivenNames" Nullable="false" Unicode="false" />
      <Property Type="DateTime" Name="DateOfBirth" Nullable="false" />
    </EntityType>
    <EntityType Name="LEARNER">
      <Key>
        <!--<PropertyRef Name="UKPRN" />-->
        <PropertyRef Name="LearnRefNumber" />
      </Key>
      <Property Name="LearnRefNumber" Nullable="false" Type="varchar2" MaxLength="12" />
      <Property Name="ULN" Type="number" Precision="10" />
      <Property Name="FamilyName" Type="varchar2" MaxLength="100" />
      <Property Name="GivenNames" Type="varchar2" MaxLength="100" />
      <Property Name="DateOfBirth" Type="date" />
     </EntityType>

LEARNER类别定义

   public partial class LEARNER
   {
    public LEARNER()
    {
        this.LEARNERCONTACTPREFERENCES = new HashSet<LEARNERCONTACTPREFERENCES>();
        this.LEARNERCONTACTs = new HashSet<LEARNERCONTACT>();
        this.LEARNERFAMs = new HashSet<LEARNERFAM>();
        this.LLDDANDHEALTHPROBS = new HashSet<LLDDANDHEALTHPROBS>();
        this.LEARNERPROVIDERSPECMONs = new HashSet<LEARNERPROVIDERSPECMON>();
        this.LEARNEREMPLOYMENTSTATUS = new HashSet<LEARNEREMPLOYMENTSTATUS>();
        this.LEARNINGDELIVERies = new HashSet<LEARNINGDELIVERY>();
    }
    public String LearnRefNumber { get; set; }
    public int ULN { get; set; }
    public string FamilyName { get; set; }
    public string GivenNames { get; set; }
    public System.DateTime DateOfBirth { get; set; }
    public short Ethnicity { get; set; }
    public string Sex { get; set; }
    public short LLDDHealthProb { get; set; }
    public string NINumber { get; set; }
    public short PriorAttain { get; set; }
    public short Accom { get; set; }
    public int ALSCost { get; set; }
    public short Dest { get; set; }
    public int UKPRN { get; set; }
    public int REC_ID { get; set; }
    public string PrevLearnRefNumber { get; set; }
    public int PrevUKPRN { get; set; }
    public short PlanLearnHours { get; set; }
    public short PlanEEPHours { get; set; }
    public virtual ICollection<LEARNERCONTACTPREFERENCES> LEARNERCONTACTPREFERENCES { get; set; }
    public virtual ICollection<LEARNERCONTACT> LEARNERCONTACTs { get; set; }
    public virtual ICollection<LEARNERFAM> LEARNERFAMs { get; set; }
    public virtual ICollection<LLDDANDHEALTHPROBS> LLDDANDHEALTHPROBS { get; set; }
    public virtual ICollection<LEARNERPROVIDERSPECMON> LEARNERPROVIDERSPECMONs { get; set; }
    public virtual ICollection<LEARNEREMPLOYMENTSTATUS> LEARNEREMPLOYMENTSTATUS { get; set; }
    public virtual ICollection<LEARNINGDELIVERY> LEARNINGDELIVERies { get; set; }
}

答案(部分)是ULN属性的数据类型(从Int32修改为Int64)。

按照建议更正了这一点,事情现在正按照我希望的方式进行。经验教训。再次感谢你的帮助。

问候,

李。

最新更新