c# - 如何修复“无法识别的 guid 格式”?
问题描述
当我尝试从 mysql 数据库中选择 Guid 时,我的 asp.net MVC 应用程序中出现“无法识别的 guid 格式”错误。
var contact_cstm = ctx.contacts_cstm.Where(x => x.national_id_c == model.NID).FirstOrDefault();
if (contact_cstm != null)
{
var contact = ctx.contacts.Where(x => x.id == contact_cstm.id_c && x.deleted==false).FirstOrDefault();
if (contact != null)
{
model.FName = contact.first_name;
}
ctx.SaveChanges();
return true;
}
在 contact_cstm 我得到 GUID 很好,
但是在第二个查询中,当我使用 where 子句时,它会显示错误。
var contact = ctx.contacts.Where(x => x.id == contact_cstm.id_c && x.deleted==false).FirstOrDefault();
我的联系人模型
public partial class contact
{
public System.Guid id { get; set; }
public string first_name { get; set; }
public string phone_mobile { get; set; }
}
堆栈跟踪:
[FormatException: Unrecognized Guid format.]
System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult& result) +12635324
System.Guid..ctor(String g) +108
MySql.Data.Types.MySqlGuid.MySql.Data.Types.IMySqlValue.ReadValue(MySqlPacket packet, Int64 length, Boolean nullVal) +205
MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue valObject) +168
MySql.Data.MySqlClient.ResultSet.ReadColumnData(Boolean outputParms) +65
MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior) +158
MySql.Data.MySqlClient.MySqlDataReader.Read() +62
MySql.Data.Entity.EFMySqlDataReader.Read() +13
System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.StoreRead() +36
[EntityCommandExecutionException: An error occurred while reading from the store provider's data reader. See the inner exception for details.]
System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.HandleReaderException(Exception e) +4816212
System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.StoreRead() +49
System.Data.Entity.Core.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +41
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() +114
System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +168
System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1(IEnumerable`1 sequence) +42
System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +61
System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +119
System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +106
System.Linq.Queryable.FirstOrDefault(IQueryable`1 source) +220
RegaSelfService.BLL.ContactBLL.updateUserIfExist(ContactModel model) in C:\inetpub\wwwroot\Rega_selfService\RegaSelfService\RegaSelfService\BLL\ContactBLL.cs:46
RegaSelfService.Controllers.<login>d__3.MoveNext() in C:\inetpub\wwwroot\Rega_selfService\RegaSelfService\RegaSelfService\Controllers\ContactController.cs:42
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +102
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +64
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97
System.Web.Mvc.Async.<>c__DisplayClass8_0.<BeginInvokeAsynchronousActionMethod>b__1(IAsyncResult asyncResult) +17
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0() +58
System.Web.Mvc.Async.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +228
System.Web.Mvc.Async.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +35
System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
解决方案
请试试这个:
var id_c = new Guid(contact_cstm.id_c);
var contact = ctx.contacts.Where(x => x.id == id_c && x.deleted==false).FirstOrDefault();
推荐阅读
- google-apps-script - 在 gmail 插件中使用动态选项列表创建选择(下拉)输入
- c# - 如何在excel中找到带有某个关键字的最后一行
- django - 如何自动更新 Django 中的字段
- amazon-web-services - AWS EMR:集群在用户请求中终止
- ubuntu - Truffle deploy 编译合约但不迁移到网络
- javascript - 汉堡菜单切换 Javascript
- session - 获取所有活动会话 asp.net core 2
- javascript - 我将如何以及在哪里将这两个变量组合在 JavaScript 中?
- azure - ComputerVision API 不允许带有 SAS 令牌的 BlobURL
- angular - Angular 7:更新服务变量时更新组件变量