c# - {"The specified cast from a materialized ' System.Guid' 类型到 'System.Int32' 类型无效。"

标签 c# asp.net entity-framework

我有一个 Entity Framework 模型,其中包含一个 Assets 表,如下所示:

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

我试图只返回“名称”列。这是我用来执行此操作的代码:

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

但是,它在定义“ Assets ”的那一行给了我这篇文章标题中引用的错误。我该如何解决这个问题?

最佳答案

GUID 不是 int,在你的数据库中,Switch 是一个 GUID。

GUID 是由 - 分割的十六进制数

以下是 GUID 的三个示例:

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

06F6D8BA-C10D-4806-B190-000006BA0513

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

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

所以你的 Assets 更像是:

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 文件始终是最新的,这是您在遇到实体错误时应该检查的内容。

这样做:

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

  2. 删除该页面内的所有内容...似乎有点可怕?在这样做之前复制您的项目,这样您就有了备份。

  3. 右键单击白色空白处的某处,然后选择从数据库更新 或类似的东西(我的 visual studio 不是英文...)

  4. 选择您需要的每个表/ View /存储过程即可

关于c# - {"The specified cast from a materialized ' System.Guid' 类型到 'System.Int32' 类型无效。",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377943/

相关文章:

c# - 在 C# 中获取 SQL 执行 cmd 持续时间

c# - EntityFramework 6.0.0.0 读取数据,但不插入

c# - 有没有办法搜索集合中的每个元素 c#

c# - 仅用于内容文件的 Nuget 包

c# - 我怎样才能用 C# 延迟?

c# - ASP.NET ListView 控件未触发 OnItemCommand 事件

c# - 如何生成一个链接,即谷歌地图搜索结果

asp.net - Excel 互操作库与 ASP.NET 不兼容?

asp.net-mvc - 错误 3027 : No mapping specified for the following EntitySet/AssociationSet - sysdiagrams(deleted the sysdiagram table by mistake)

c# - 绑定(bind)到一列数据网格并应用样式