{“从实例化的'System.Guid'类型到'System.Int32'类型的指定转换无效。” [英] {"The specified cast from a materialized 'System.Guid' type to the 'System.Int32' type is not valid."

查看:156
本文介绍了{“从实例化的'System.Guid'类型到'System.Int32'类型的指定转换无效。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含单个资产表的实体框架模型,如下所示:

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?

推荐答案

GUID不是int,在您的数据库中,Switch是GUID。

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

GUID是一个十六进制数字,除以-

A GUID is an hexadecimal number splited by -

以下是GUID的三个示例:

Here's three examples of GUID :

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

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

06F6D8BA-C10D-4806-B190-000006BA0513

06F6D8BA-C10D-4806-B190-000006BA0513

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

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

一个int不能包含字母,也不能包含-

An int cannot contains letters, neither can it contains -

所以您的资产更像是:

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

还要确保edmx文件始终是最新的您应该在遇到实体错误时进行检查。

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

为此:


  1. 在您的项目中找到您的.edmx文件(您可能称为acls3Entities.edmx)

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

删除该页面内的所有内容……似乎蠕动一下?复制项目,然后再备份。

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

右键单击空白处的某个位置,然后选择从数据库中更新或类似的内容(我的视觉工作室不是英语...)

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

选择每个表/查看/存储所需的过程,仅此而已

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

这篇关于{“从实例化的'System.Guid'类型到'System.Int32'类型的指定转换无效。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆