我试图用以下函数从数据库中获取一个'DocumentationPage‘类型的对象:
CurrentDocumentationPage = await documentationPageService.GetDocumentationPageById(DocumentID);
在这种情况下,我使用
[Parameter] public Guid DocumentID { get; set; }
这将与一个NavigateTo
函数一起传递。
public void OnNavItemClick(Guid Id)
{
navMan.NavigateTo($"/View/" + Id);
}
用相应的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; }
}
如果实体是:
public class Entity
{
public Guid Id;
}
现在应该发生以下情况:
function.
OnClick
Guid ID
中传递的Guid ID
的NavMenu项,该函数具有传递的ID:CurrentDocumentationPage = await documentationPageService.GetDocumentationPageById(DocumentID);
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
。
当没有意义时,我试着用它作为字符串。
这是我的书签:
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的任何东西。
这对我来说毫无意义,因为它为什么会有这种方法的问题。
发布于 2021-11-18 10:47:07
Llama给出了答案,我的数据库是为UserID
建立的Guid
。但是在我的DocumentationPage
类中,它是一个string
而不是Guid
https://stackoverflow.com/questions/70018293
复制相似问题