简体   繁体   English

实体框架使用mvc——数据注解

[英]Entity Framework using mvc - data annotation

I have connected to SQL database tables and I am trying to create data annotations for string CountryName in the model's folder.我已连接到 SQL 数据库表,并且正在尝试为模型文件夹中的字符串 CountryName 创建数据注释。 on the web when it gives me a error:在网络上,当它给我一个错误时:

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'. 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; }
    }
}

You are setting the StringLength attribute to a property of type Nullable<int> .您正在将StringLength属性设置为Nullable<int>类型的属性。 Hence, it tries to cast the int to string.因此,它尝试将 int 转换为 string。 You probably intended to put the attribute to the CountryName property as follows:您可能打算将属性放入CountryName属性,如下所示:

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

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

Here you are telling EntityFramework the expected string will be 50, while the datatype is of Nullable<int> .在这里,您告诉 EntityFramework 预期的字符串将为 50,而数据类型为Nullable<int>

Annotations should be on top of the property they "describe", not below, as it seems this 50 limit would be suitable for CountryName instead of ContinentID?注释应该在他们“描述”的属性之上,而不是之下,因为这 50 个限制似乎适合 CountryName 而不是 ContinentID?

If you wanna restrict the string field you should add data annotation over the field如果你想限制字符串字段,你应该在字段上添加数据注释

[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; }

You decorate your Nullable Property ContentID with [StringLength(50)] what is only possible for Strings.您使用 [StringLength(50)] 装饰您的 Nullable 属性 ContentID,这仅适用于字符串。

Data annotations work for the property after them, so you should just move you [StringLength(50)] annotation 2 lines higher数据注释适用于它们之后的属性,因此您应该将 [StringLength(50)] 注释移高 2 行

... ...

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

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM