Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{"The specified cast from a materialized 'System.Guid' type to the 'System.Int32' type is not valid."

I have an Entity Framework Model which contains a single table of Assets as follows:

public partial class Asset
{
    public int Switch { get; set; }
    public string Port { get; set; }
    public string Name { get; set; }
    public string Connection { get; set; }
    public string Interface { get; set; }
    public string ConnectionUser { get; set; }
    public string ConnectionDate { get; set; }
}

I am trying to only return the 'Name' column. This is the code I am using to do so:

    // create the entity model for DB operations
    acls3Entities entities = new acls3Entities();
    List<Asset> assets = entities.Assets.ToList();

    foreach (Asset asset in assets)
    {
        Console.WriteLine(string.Format("{0}", asset.Name));
    };

However it is giving me the error referenced in the title of this post at the line that defines 'assets'. How can I solve this issue?

like image 474
CircAnalyzer Avatar asked Jul 14 '16 15:07

CircAnalyzer


1 Answers

The GUID is not an int, in your database, Switch is a GUID.

A GUID is an hexadecimal number splited by -

Here's three examples of GUID :

839E4FB1-F5F5-4C46-AFD1-000002CC625F

06F6D8BA-C10D-4806-B190-000006BA0513

2D3343FD-3E8A-4B33-9198-00002031F5F8

An int cannot contains letters, neither can it contains -

So your asset is more like :

public partial class Asset
{
public Guid Switch { get; set; }  // here <- GUID
public string Port { get; set; }
public string Name { get; set; }
public string Connection { get; set; }
public string Interface { get; set; }
public string ConnectionUser { get; set; }
public string ConnectionDate { get; set; }
}

Also make sure your edmx file is always up to date, this is something you should check whenever you encounter a bug with entity.

To do so :

  1. Find your .edmx file in your project (yours is probably called acls3Entities.edmx)

  2. Delete EVERYTHING inside that page... it seems frigthening a bit ? copy your project before doing so, this way you'll have a back up.

  3. Right click somewhere in the white void, and choose Update from database or something that looks like that (my visual studio is not english... )

  4. Choose every table / view / stored procedure you need and that's all

like image 95
Antoine Pelletier Avatar answered Sep 20 '22 13:09

Antoine Pelletier