【问题标题】:Entity Framework using mvc - data annotation使用 mvc 的实体框架 - 数据注释
【发布时间】:2020-09-14 11:18:00
【问题描述】:

我已连接到 SQL 数据库表,我正在尝试为模型文件夹中的字符串 CountryName 创建数据注释。当它给我一个错误时在网络上:

System.InvalidCastException:无法将“System.Int32”类型的对象转换为“System.String”类型。

namespace WorldEventsWeb.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class tblCountry
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public tblCountry()
        {
            this.tblEvents = new HashSet<tblEvent>();
        }
    
        public int CountryID { get; set; }
        public string CountryName { get; set; }
        [StringLength(50)]
        public Nullable<int> ContinentID { get; set; }
    
        public virtual tblContinent tblContinent { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<tblEvent> tblEvents { get; set; }
    }
}

【问题讨论】:

  • 为什么要在 Nullable&lt;int&gt; 类型上使用 StringLength 属性?属性高于它们所描述的类型,而不是低于它。
  • 在注释字段之前使用注释:[StringLength(50)] public string CountryName { get; set; }

标签: c# asp.net-mvc entity-framework model-view-controller visual-studio-2019


【解决方案1】:

您正在将StringLength 属性设置为Nullable&lt;int&gt; 类型的属性。因此,它尝试将 int 转换为字符串。您可能打算将属性放在 CountryName 属性中,如下所示:

[StringLength(50)]
public string CountryName { get; set; }

public Nullable<int> ContinentID { get; set; }

【讨论】:

    【解决方案2】:
    [StringLength(50)]
    public Nullable<int> ContinentID { get; set; }
    

    这里你告诉 EntityFramework 预期的字符串是 50,而数据类型是 Nullable&lt;int&gt;

    注释应该在他们“描述”的属性之上,而不是在下面,因为这 50 个限制似乎适合 CountryName 而不是 ContinentID?

    【讨论】:

      【解决方案3】:

      如果你想限制字符串字段,你应该在字段上添加数据注释

      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
              public tblCountry()
              {
                  this.tblEvents = new HashSet<tblEvent>();
              }
          
              public int CountryID { get; set; }
              [StringLength(50)]
              public string CountryName { get; set; }
              public Nullable<int> ContinentID { get; set; }
          
              public virtual tblContinent tblContinent { get; set; }
              [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
              public virtual ICollection<tblEvent> tblEvents { get; set; }
      

      【讨论】:

        【解决方案4】:

        您可以使用 [StringLength(50)] 来装饰您的 Nullable 属性 ContentID,这仅适用于字符串。

        数据注释适用于它们之后的属性,因此您只需将 [StringLength(50)] 注释移高 2 行

        ...

        public int CountryID { get; set; }
        [StringLength(50)]
        public string CountryName { get; set; }
        public Nullable<int> ContinentID { get; set; }
        

        ...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-10
          相关资源
          最近更新 更多