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

Row*_*win 1 .net c# asp.net-identity blazor

我试图使用以下函数从数据库中获取“DocumentationPage”类型的对象:

CurrentDocumentationPage = await documentationPageService.GetDocumentationPageById(DocumentID);
Run Code Online (Sandbox Code Playgroud)

在这种情况下我正在使用

[Parameter] public Guid DocumentID { get; set; }
Run Code Online (Sandbox Code Playgroud)

NavigateTo这与函数一起传递

public void OnNavItemClick(Guid Id)
        {
            navMan.NavigateTo($"/View/" + Id);
        }
Run Code Online (Sandbox Code Playgroud)

要从我的数据库中获取相应的 Guid。我的DocumentationPage课程如下所示:

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; }
    }
Run Code Online (Sandbox Code Playgroud)

实体所在位置:

public class Entity
    {
        public Guid Id;
    }
Run Code Online (Sandbox Code Playgroud)

现在应该发生以下情况:

  • Guid ID单击保存有一个函数中的 NavMenu 项OnClick
  • 使用已传递的 ID 触发函数:
CurrentDocumentationPage = await documentationPageService.GetDocumentationPageById(DocumentID);
Run Code Online (Sandbox Code Playgroud)
  • 之后到达方法:
public async Task<DocumentationPage> GetByIdAsync(Guid id)
        {
            DocumentationPage documentationPage = context.DocumentationPages.FirstOrDefault(c => c.Id == id);

            return documentationPage;
        }
Run Code Online (Sandbox Code Playgroud)

此时提示我错误 Unable to cast object of type 'System.Guid' to type 'System.String

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

这是我的堆栈跟踪:

  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
Run Code Online (Sandbox Code Playgroud)

现在,我确实发现 at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i) 这两种方式都没有意义,但据我所知,我没有更改有关 SqlDataReader 的任何内容。

对于我来说,为什么这种方法有问题是没有意义的。

Row*_*win 5

Llama 给出了答案,我的数据库被设置UserIDGuid. 但在我的DocumentationPage课堂上,它是一个string而不是Guid