2016-07-14 79 views
4

我有一个实体框架模型包含资产的单个表如下:{“从物化”System.Guid“类型到”System.Int32“类型的指定强制转换无效。”

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

但是它给我在这个岗位在限定的资产“行的标题中引用的错误。我该如何解决这个问题?

+1

看看在你的数据库交换机列的类型,并更改为INT如果它是GUID。 – Kami

回答

5

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

的GUID被一个十六进制数由splited -

这里的GUID的三个例子:

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

06F6D8BA-C10D-4806-B190-000006BA0513

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

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

所以,你的资产更像是:

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. 删除页面内一切...这似乎frigthening有点?在这样做之前复制你的项目,这样你就可以备份了。

  3. 右键点击某处白空白,并选择Update from database或东西,看起来像(我的Visual Studio是不是英语......)

  4. 选择的每个表/视图/存储需要的过程,这就是全部

+0

刚刚尝试了上面的答案,并得到以下错误:其他信息:概念侧面类型'acls3Model.Asset'中成员'Switch'的类型'Edm.Int32'与类型'System'不匹配。Guid'成员'Switch'对象端类型 – DeeTee

+3

下摆...也许只是更新你的edmx文件...如果你知道如何......如果我不能解释 –

0

I solved this problem by using查询条件两端的ToString()函数。

初值问题:

Guid userId = Guid.Parse(User.Identity.GetUserId());  
candidateDashBoard.DismissalRecord = db.DismissalRecords.Where(c => 
        c.CandidateId == userId).FirstOrDefault(); 

解决方案:

Guid userId = Guid.Parse(User.Identity.GetUserId()); 
candidateDashBoard.DismissalRecord = db.DismissalRecords.Where(c => 
        c.CandidateId.ToString() == userId.ToString()).FirstOrDefault(); 
相关问题