首页 > 解决方案 > 实体框架 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 转换为字符串。看起来它应该很简单,但它引起了很多头痛。

标签: c#entity-frameworkasynchronous

解决方案


推荐阅读