System.InvalidCastException occurred in EntityFramework.Core.dll

asp.net-core entity-framework-core linq-to-entities

Question

The below is the the Application Entity:

public class Application
{
    [Key]
    [Column(TypeName = "integer")]
    public int ApplicationID { get; set; }

    [Required]
    [Column(TypeName = "nvarchar(50)")]
    public string ApplicationName { get; set; }

    [Column(TypeName = "nvarchar(150)")]
    public string Description { get; set; }

    [Required]
    [ForeignKey("mTechnology")]
    [Column(TypeName = "integer")]
    public int TechnologyID { get; set; }

    [Required]
    [Column(TypeName = "int")]
    public string CreatedBy { get; set; }

    [Required]
    [Column(TypeName = "datetime")]
    public DateTime CreatedDate { get; set; }

    public virtual mTechnology Technology { get; set; }
}

On executing the below LINQ query, I am getting an error at run-time in the foreach loop

List<int> technologyList = new List<int>();
var query = from a in _mApplicationDbContext.Applications
        group a.Technology by a.TechnologyID into g
        select new
        {
            TechnologyID = g.Key
        };

foreach (var item in query)
{
    technologyList.Add(item.TechnologyID);
}

The below is the error message:

An exception of type 'System.InvalidCastException' occurred in EntityFramework.Core.dll but was not handled in user code

Additional information: Unable to cast object of type 'System.Int32' to type 'System.String'.

Is the LINQ query wrong or any other error?

Accepted Answer

By the looks of things you've made a mistake in you're model.

You're attempting to use an invalid datatype when you're defining your foreign key TechnologyID as an integer. With out knowing what type of database you're using, I'm assuming you're using sql server of some flavour, in which case the data type "integer" doesn't exist.

Bellow should resolve the issue:

public class Application
{
    [Key]
    [Column]
    public int ApplicationID { get; set; }

    [Required]
    [Column(TypeName = "nvarchar(50)")]
    public string ApplicationName { get; set; }

    [Column(TypeName = "nvarchar(150)")]
    public string Description { get; set; }

    [Required]
    [ForeignKey("mTechnology")]
    [Column]
    public int TechnologyID { get; set; }

    [Required]
    [Column(TypeName = "int")]
    public string CreatedBy { get; set; }

    [Required]
    [Column]
    public DateTime CreatedDate { get; set; }

    public virtual mTechnology Technology { get; set; }
}

As a side note you don't always need to specify the typename for all of you're properties. If you have an int property and are mapping it to an int column you can just use the [Column] attribute and EF will use the correct int type. Specifying the type name is more important when you're trying to use a long in your model when there isn't a long in sql server so you'd use the TypeName property as [Column(TypeName = "bigint")]



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why