specified cast is not valid linq

The error

specified cast is not valid linq

can come from the fact the column types do not match between the class and the name attribute of that class.

Example : NorthWnd Database

The territories table in NorthWnd database has three field

TerritoryID nvarchar(20)
TerritoryDescription nchar(50)
RegionID int

If you map this to LINQ using the following class mapping

[Table(Name = "Territories")]
        public class Territory
            public int TerritoryID; # error is here. In DB this field is nvarchar
            public string TerritoryDescription;
            public int RegionID;

Change int TerritoryID to String will fix the problem.

Leave a Reply

Your email address will not be published. Required fields are marked *