c# - 实体框架 Guid 到带有连接表的字符串
问题描述
我在网上尝试了很多建议,但没有任何效果。我正在尝试使用实体框架从数据库中返回值,但我的 TransactionId 是一个 GUID。该代码在尝试将 GUID 转换为字符串时引发错误。这是我最初的:
var entityVerification = from tl in _context.TransactionLogs
join ts in _context.TransactionStatuses on tl.GUTransactionId equals ts.GUTransactionId
where tl.ExternalTargetAccountNumber == accountNumber
orderby ts.PostedTime descending
select new TransactionHistoryEntity
{
TransactionId = tl.GUTransactionId,
SourceAccountNumber = tl.ExternalSourceAccountNumber,
ActionCode = tl.ActionCode,
Amount = tl.Amount,
CurrencyCode = tl.CurrencyCode,
PostedTime = ts.PostedTime,
StatusCode = ts.StatusCode
};
return await entityVerification.ToListAsync();
我尝试创建一个查询对象,然后我可以稍后将 TransactionId 转换为字符串,但这会引发相同的错误:
var transactionLogs = _context.TransactionLogs;
var transactionStatus = _context.TransactionStatuses;
var test = transactionLogs.ToList().Join(transactionStatus, ts => ts.GUTransactionId, tl => tl.GUTransactionId,
(tl, ts) => new
{
ts.GUTransactionId,
tl.ExternalSourceAccountNumber,
tl.ActionCode,
tl.Amount,
tl.CurrencyCode,
ts.PostedTime,
ts.StatusCode
});
这是堆栈跟踪:
at System.Data.SqlClient.SqlBuffer.get_String()
at lambda_method(Closure , DbDataReader )
at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.BufferlessMoveNext(DbContext _, Boolean buffer, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable`1 source, TAccumulate seed, Func`3 accumulator, Func`2 resultSelector, CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Aggregate.cs:line 120
at DataManagement.Data.Repository.FTTransact.TransactionLogRepository.GetTransactionHistory(String accountNumber) in D:\Source\DayforceSpecialProjects\Fintech\Platform\DataManagement\Data\Repository\FTTransact\TransactionLogRepository.cs:line 39
语境:
modelBuilder.Entity<TransactionLogEntity>(entity =>
{
entity.ToTable("TransactionLog");
entity.HasKey("GUTransactionId");
entity.Property(e => e.GlbGuProgramId)
.HasColumnName("GLB_GUProgramId")
.HasColumnType("bigint");
entity.Property(e => e.GlbGuProductId)
.HasColumnName("GLB_GUProductId")
.HasColumnType("bigint");
entity.Property(e => e.Amount)
.HasColumnType("decimal(18,10)");
entity.Property(e => e.CurrencyCode)
.HasColumnType("nvarchar(10)");
entity.Property(e => e.Units)
.HasColumnType("decimal(18,10)");
entity.Property(e => e.ValuePerUnit)
.HasColumnType("decimal(18,10)");
entity.Property(e => e.TransactionReceivedTime)
.HasColumnType("datetime");
entity.Property(e => e.TargetPostingTime)
.HasColumnType("datetime");
entity.Property(e => e.RequestTransactionId)
.HasColumnType("nvarchar(128)");
entity.Property(e => e.ExternalTargetAccountNumber)
.HasColumnType("nvarchar(128)");
entity.Property(e => e.ExternalSourceAccountNumber)
.HasColumnType("nvarchar(128)");
entity.Property(e => e.TargetRegGuAccountId)
.HasColumnName("TargetREG_GUAccountId")
.HasColumnType("bigint");
entity.Property(e => e.SourceRegGuAccountId)
.HasColumnName("SourceREG_GUAccountId")
.HasColumnType("bigint");
entity.Property(e => e.ActionCode)
.HasColumnType("nvarchar(64)");
});
modelBuilder.Entity<TransactionStatusEntity>(entity =>
{
entity.ToTable("TransactionStatus");
entity.HasKey("GUTransactionStatusId");
entity.Property(e => e.GUTransactionId)
.HasColumnType("guid");
entity.Property(e => e.PostedTime)
.HasColumnType("datetime");
entity.Property(e => e.StatusCode)
.HasColumnType("nchar(10)");
});
实体:
public class TransactionStatusEntity
{
public string GUTransactionStatusId { get; set; }
public string GUTransactionId { get; set; }
public DateTime PostedTime { get; set; }
public string StatusCode { get; set; }
}
public class TransactionLogEntity
{
public string GUTransactionId { get; set; }
public long GlbGuProgramId { get; set; }
public long GlbGuProductId { get; set; }
public decimal Amount { get; set; }
public string CurrencyCode { get; set; }
public decimal Units { get; set; }
public decimal? ValuePerUnit { get; set; }
public DateTime TransactionReceivedTime { get; set; }
public DateTime TargetPostingTime { get; set; }
public string RequestTransactionId { get; set; }
public string ExternalTargetAccountNumber { get; set; }
public string ExternalSourceAccountNumber { get; set; }
public long? TargetRegGuAccountId { get; set; }
public long? SourceRegGuAccountId { get; set; }
public string ActionCode { get; set; }
}
我只需要将 GUID 转换为字符串。看起来它应该很简单,但它引起了很多头痛。
解决方案
推荐阅读
- php - 在 foreach 中 curl 上传文件更改我的帖子值
- java - Java - CommandSender 类型未定义适用于此处的 getName(Object)
- php - 对 WordPress 网页上目录中的所有图像进行分页
- android - 在 LinearLayout 上添加可绘制背景时,API 16 Phone 上的应用程序崩溃
- javascript - 发送内容到字符串,编辑它,然后替换它
- java - 当我在我的 android 应用程序中测试 PHP 会话变量时,它没有设置
- c# - 简单的触摸控制
- angular - Angular 6 - 一个输入中的更改值更改多个输入中的值
- c# - 更改 UWP/XAML 中的复选框以在错误时显示十字
- scons - Scons:如何将决策者分配给特定节点?