首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法将“System.Guid”类型的对象强制转换为“System.String”类型,而我只使用Guid

无法将“System.Guid”类型的对象强制转换为“System.String”类型,而我只使用Guid
EN

Stack Overflow用户
提问于 2021-11-18 10:34:34
回答 1查看 3.9K关注 0票数 1

我试图用以下函数从数据库中获取一个'DocumentationPage‘类型的对象:

代码语言:javascript
运行
复制
CurrentDocumentationPage = await documentationPageService.GetDocumentationPageById(DocumentID);

在这种情况下,我使用

代码语言:javascript
运行
复制
[Parameter] public Guid DocumentID { get; set; }

这将与一个NavigateTo函数一起传递。

代码语言:javascript
运行
复制
public void OnNavItemClick(Guid Id)
        {
            navMan.NavigateTo($"/View/" + Id);
        }

用相应的Guid从我的数据库中得到这个。我的DocumentationPage类如下所示:

代码语言:javascript
运行
复制
public class DocumentationPage : Entity
    {
        public string UserId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public DateTime Published { get; set; }
        public DateTime Updated { get; set; }
    }

如果实体是:

代码语言:javascript
运行
复制
public class Entity
    {
        public Guid Id;
    }

现在应该发生以下情况:

function.

  • Fires
  • 单击要在OnClick Guid ID中传递的Guid ID的NavMenu项,该函数具有传递的ID:

代码语言:javascript
运行
复制
CurrentDocumentationPage = await documentationPageService.GetDocumentationPageById(DocumentID);

  • 之后,它到达了方法:

代码语言:javascript
运行
复制
public async Task<DocumentationPage> GetByIdAsync(Guid id)
        {
            DocumentationPage documentationPage = context.DocumentationPages.FirstOrDefault(c => c.Id == id);

            return documentationPage;
        }

它提示我使用错误Unable to cast object of type 'System.Guid' to type 'System.String

当没有意义时,我试着用它作为字符串。

这是我的书签:

代码语言:javascript
运行
复制
  HResult=0x80004002
  Message=Unable to cast object of type 'System.Guid' to type 'System.String'.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Runtime.CompilerServices.CastHelpers.ChkCast_Helper(Void* toTypeHnd, Object obj)
   at Microsoft.Data.SqlClient.SqlBuffer.get_String()
   at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.First[TSource](IQueryable`1 source)
   at TerceraDeveloperSite.Repositories.DocumentationPageRepository.<GetByIdAsync>d__2.MoveNext() in C:\Users\Rowin\source\repos\TerceraDeveloperSite\Repositories\DocumentationPageRepository.cs:line 21

  This exception was originally thrown at this call stack:
    [External Code]
    TerceraDeveloperSite.Repositories.DocumentationPageRepository.GetByIdAsync(System.Guid) in DocumentationPageRepository.cs

现在,我确实找到了at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i),但是据我所知,我并没有改变有关SqlDataReader的任何东西。

这对我来说毫无意义,因为它为什么会有这种方法的问题。

EN

Stack Overflow用户

回答已采纳

发布于 2021-11-18 10:47:07

Llama给出了答案,我的数据库是为UserID建立的Guid。但是在我的DocumentationPage类中,它是一个string而不是Guid

票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70018293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档