我有一个 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 文件始终是最新的,这是您在遇到实体错误时应该检查的内容。
这样做:
在您的项目中找到您的 .edmx 文件(您的文件可能名为 acls3Entities.edmx)
删除该页面内的所有内容...似乎有点可怕?在这样做之前复制您的项目,这样您就有了备份。
右键单击白色空白处的某处,然后选择
从数据库更新
或类似的东西(我的 visual studio 不是英文...)选择您需要的每个表/ View /存储过程即可
关于c# - {"The specified cast from a materialized ' System.Guid' 类型到 'System.Int32' 类型无效。",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377943/