Samkelo Nhlapo
Samkelo Nhlapo

Reputation: 23

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. on the web when it gives me a error:

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

Upvotes: 0

Views: 102

Answers (4)

Myko
Myko

Reputation: 59

You decorate your Nullable Property ContentID with [StringLength(50)] what is only possible for Strings.

Data annotations work for the property after them, so you should just move you [StringLength(50)] annotation 2 lines higher

...

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

...

Upvotes: 0

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

Upvotes: 0

kaffekopp
kaffekopp

Reputation: 2619

You are setting the StringLength attribute to a property of type Nullable<int>. Hence, it tries to cast the int to string. You probably intended to put the attribute to the CountryName property as follows:

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

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

Upvotes: 1

Jens Stragier
Jens Stragier

Reputation: 223

[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>.

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?

Upvotes: 0

Related Questions