[英]EF Core 'InMemoryProjectionBindingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core
[英]Processing of the LINQ expression ... failed. This may indicate either a bug or a limitation in EF Core
“'NavigationExpandingExpressionVisitor' 處理 LINQ 表達式 'DbSet<Log_Service_ExportToFile>.ElementAt(__p_0)' 失敗。這可能表示 EF Core 中存在錯誤或限制。”
我在使用 Entity Framework Core 3.0 的應用程序中遇到此錯誤。 從我的代碼:
private void UpdateLogService(DateTime t)
{
var Log_Service_ExportToFile_List = dbContext.Log_Service_ExportToFile.ToList();
bool found = false;
for (int i = 0; i < Log_Service_ExportToFile_List.Count(); i++)
{
var s = Log_Service_ExportToFile_List[i];
if (s.MandantId == Config.MandantId && s.ForWhichTime == t)
{
dbContext.Log_Service_ExportToFile.ElementAt(i).UTC = DateTime.Now;
dbContext.SaveChanges();
found = true;
break;
}
}
if (found) return;
var logservice = new Log_Service_ExportToFile
{
Id = Guid.NewGuid(),
UTC = DateTime.Now,
FileName = Config.Path,
MandantId = Config.MandantId,
ForWhichTime = t
};
dbContext.Log_Service_ExportToFile.Add(logservice);
dbContext.SaveChanges();
}
編譯器哭泣的行是:
dbContext.Log_Service_ExportToFile.ElementAt(i).UTC = DateTime.Now;
class Log_Service_ExportToFile定義如下:
public class Log_Service_ExportToFile
{
public Log_Service_ExportToFile()
{
}
[Key]
public int Id { get; set; }
public DateTime UTC { get; set; }
public string FileName { get; set; }
public string MandantId { get; set; }
public DateTime ForWhichTime { get; set; }
}
在繼承自 DbContext 的 class 中,我定義了:
public virtual DbSet<Log_Service_ExportToFile> Log_Service_ExportToFile { get; set; }
modelBuilder.Entity<Log_Service_ExportToFile>(entity =>
{
entity.ToTable("LOG_SERVICE_EXPORTTOFILE");
entity.Property(e => e.UTC).HasColumnType("datetime");
entity.Property(e => e.FileName).HasColumnType("ntext");
entity.Property(e => e.MandantId).HasColumnType("ntext");
entity.Property(e => e.ForWhichTime).HasColumnType("datetime");
});
在使用ElementAt
之前將dbContext.Log_Service_ExportToFile
轉換為List
應該可以解決問題。
嘗試轉換
dbContext.Log_Service_ExportToFile.ElementAt(i).UTC = DateTime.Now;
到
dbContext.Log_Service_ExportToFile.ToList().ElementAt(i).UTC = DateTime.Now;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.