C# 使用mvc的实体框架-数据注释
我已经连接到SQL数据库表,并且正在尝试为模型文件夹中的字符串CountryName创建数据注释。在web上显示错误时: System.InvalidCastException:无法将类型为“System.Int32”的对象强制转换为类型为“System.String”C# 使用mvc的实体框架-数据注释,c#,asp.net-mvc,entity-framework,model-view-controller,visual-studio-2019,C#,Asp.net Mvc,Entity Framework,Model View Controller,Visual Studio 2019,我已经连接到SQL数据库表,并且正在尝试为模型文件夹中的字符串CountryName创建数据注释。在web上显示错误时: System.InvalidCastException:无法将类型为“System.Int32”的对象强制转换为类型为“System.String” 名称空间WorldEventsWeb.Models { 使用制度; 使用System.Collections.Generic; 使用System.ComponentModel.DataAnnotations; 公共部分类TBL国
名称空间WorldEventsWeb.Models
{
使用制度;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
公共部分类TBL国家
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共TBL国家()
{
this.tblEvents=new HashSet();
}
public int CountryID{get;set;}
公共字符串CountryName{get;set;}
[长度(50)]
可为空的公共ID{get;set;}
公共虚拟TBLContent TBLContent{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection tLevents{get;set;}
}
}
[StringLength(50)]
可为空的公共ID{get;set;}
在这里,您告诉EntityFramework预期的字符串将是50,而数据类型为Nullable
注释应该位于它们“描述”的属性之上,而不是之下,因为这50个限制似乎适用于CountryName而不是ContinentID?您正在将StringLength
属性设置为Nullable
类型的属性。因此,它尝试将int转换为string。您可能打算将该属性放入CountryName属性,如下所示:
[StringLength(50)]
public string CountryName { get; set; }
public Nullable<int> ContinentID { get; set; }
[StringLength(50)]
公共字符串CountryName{get;set;}
可为空的公共ID{get;set;}
如果要限制字符串字段,应在字段上添加数据注释
[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; }
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共TBL国家()
{
this.tblEvents=new HashSet();
}
public int CountryID{get;set;}
[长度(50)]
公共字符串CountryName{get;set;}
可为空的公共ID{get;set;}
公共虚拟TBLContent TBLContent{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection tLevents{get;set;}
您可以使用[StringLength(50)]装饰您的可空属性ContentID,这是仅对字符串可能的
数据注释适用于后面的属性,因此您只需将[StringLength(50)]注释向上移动两行即可
public int CountryID{get;set;}
[长度(50)]
公共字符串CountryName{get;set;}
可为空的公共ID{get;set;}
…为什么对类型为可空的
使用StringLength
属性?属性位于它们所描述的类型的上方,而不是下方。请在注释字段前面使用注释:[StringLength(50)]公共字符串CountryName{get;set;}
[StringLength(50)]
public string CountryName { get; set; }
public Nullable<int> ContinentID { get; set; }
[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; }
public int CountryID { get; set; }
[StringLength(50)]
public string CountryName { get; set; }
public Nullable<int> ContinentID { get; set; }