代码之家  ›  专栏  ›  技术社区  ›  Hamza

{“无法将类型为'System.String'的对象强制转换为类型为'System.Guid'。”}System。InvalidCast异常

  •  1
  • Hamza  · 技术社区  · 2 年前

    这是我服务的方法。

    public async Task<InvitedUser> CheckEmail(string Email)
    {
        var result = await _db.InvitedUser.SingleOrDefaultAsync(_ => _.Email == Email);
        return result;
    }
    

    受邀用户的架构为:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    
    namespace Project.Core.Entities
    {
        [Table("InvitedUser")]
        public class InvitedUser
        {
            [Key]
            public Guid UserAccountId { get ; set; }
            public string Email { get; set; }
            public Guid ReferrerAccountId { get ; set; }    
            public string Name { get; set; }    
            public string AccessCode { get; set; }
            public string Status { get; set; }
            public DateTime CreatedAt { get; set; }         
        }
    }
    

    现在,每当我执行上述LINQ查询时,都会出现以下错误

    {“无法将类型为'System.String'的对象强制转换为类型为'System.Guid'。”}

    我也试过了

    var result = _db.InvitedUser.Where(i => i.Email== Email)=.FirstOrDefault()
    

    仍然没有进步。

    EF威瑞森:5.0.9 C#版本:3.1

    0 回复  |  直到 2 年前
        1
  •  3
  •   thejrprogrammer    2 年前

    ReferrerAccountId和UserAccountId字段类型是字符串“varchar”。但模型中的属性具有Guid类型。

    所以 您应该将表中的字符串字段保存为uniqueidentifier类型 或 在模型中将它们定义为字符串(而不是Guid)。