为什么我在EF Core 6的新构建中看到“数据为空”的查询?

huangapple go评论99阅读模式
英文:

Why am I seeing "Data is Null" on query in EF Core 6 on a new build?

问题

我正在使用EF Core 6构建一个新的应用程序。我有一个表格,tblAttendance

  1. public partial class tblAttendance
  2. {
  3. public long id { get; set; }
  4. public string? UserName { get; set; }
  5. public DateTime LoginDate { get; set; }
  6. public TimeSpan LoginTime { get; set; }
  7. public DateTime LogoutDate { get; set; }
  8. public TimeSpan LogoutTime { get; set; }
  9. public string? ComputerName { get; set; }
  10. public string? LogonServer { get; set; }
  11. }

当我执行Select All查询时:

  1. var attendanceData = dbContext.tblAttendances
  2. .Select(a => new AttendanceData
  3. {
  4. UserName = a.UserName ?? "",
  5. LoginDate = a.LoginDate,
  6. LoginTime = a.LoginTime,
  7. LogoutDate = a.LogoutDate,
  8. LogoutTime = a.LogoutTime,
  9. ComputerName = a.ComputerName ?? "",
  10. LogonServer = a.LogonServer ?? ""
  11. });

查询返回正确的结果。

但是当我尝试过滤数据时,我收到一个"Data is Null"错误。

我已经修改了我的代码,使用了一个简单的FromSqlRaw语句:

  1. string query = @$"Select * from tblAttendance ";
  2. if (!string.IsNullOrEmpty(userName))
  3. {
  4. if (query.IndexOf("where") < 0) query += "where ";
  5. query +=$"UserName = '{userName}' ";
  6. }
  7. if (!string.IsNullOrEmpty(computerName))
  8. {
  9. if (query.IndexOf("where") < 0) query += "where "; else query += "AND ";
  10. query += $"ComputerName = '{computerName}' ";
  11. }
  12. if (loginDate.Ticks>0)
  13. {
  14. var year=loginDate.Year; var month=loginDate.Month;
  15. if (query.IndexOf("where") < 0) query += "where "; else query += "AND ";
  16. query += $" DATEPART(year, loginDate) = {year} and DATEPART(month, loginDate) = {month}";
  17. }
  18. var results=dbContext.tblAttendances.FromSqlRaw(query).ToList();

但是这也会导致"Data is Null"错误,尽管在服务器资源管理器中运行时它返回正确的结果。

我的数据上下文生成器(data context builder)如下:

  1. modelBuilder.Entity<tblAttendance>(entity =>
  2. {
  3. entity.HasKey(e => e.id).HasName("PK_tblAttendanceScript");
  4. entity.ToTable("tblAttendance");
  5. entity.HasIndex(e => new { e.UserName, e.LoginDate }, "UserNames_tblAttendanceScript");
  6. entity.Property(e => e.ComputerName).HasMaxLength(50);
  7. entity.Property(e => e.LoginDate).HasColumnType("date");
  8. entity.Property(e => e.LogonServer).HasMaxLength(50);
  9. entity.Property(e => e.LogoutDate).HasColumnType("date");
  10. entity.Property(e => e.UserName).HasMaxLength(50);
  11. });

作为EF(和Core)的新手,我现在不确定该从哪里开始查找问题。简短的在线搜索没有找到我可以应用的解决方法。

******************** 编辑添加示例数据 ***************************

这是我直接从数据库中获取的查询结果示例:

  1. 749405 aanderso 2023-06-29 08:58:48.0000000 2023-06-29 16:05:17.0000000 MCA-TS24VM 192.168.100.90
  2. 749423 aanderso 2023-06-29 15:00:20.0000000 2023-06-29 16:05:17.0000000 MCA-TS25VM 192.168.100.97
  3. 762067 aanderso 2023-06-29 08:58:48.0000000 2023-06-29 16:05:17.0000000 MCA-TS24VM 192.168.100.90
  4. 724075 abala 2023-06-01 07:34:42.0000000 2023-06-01 NULL MCA-TS22VM 192.168.100.79
  5. 734462 abala 2023-06-01 07:34:42.0000000 2023-06-01 18:23:26.0000000 MCA-TS22VM 192.168.100.79
  6. 747249 abala 2023-06-01 07:34:42.0000000 2023-06-01 18:23:26.0000000 MCA-TS22VM 192.168.100.79
  7. 759911 abala 2023-06-01 07:34:42.0000000 2023-06-01 18:23:26.0000000 MCA-TS22VM 192.168.100.79

所使用的查询是:

  1. Select * from tblAttendance where DATEPART(year, loginDate) = 2023 and DATEPART(month, loginDate) = 6
英文:

I am building a new application using EF Core 6. I have a table, tblAttendance

  1. public partial class tblAttendance
  2. {
  3. public long id { get; set; }
  4. public string? UserName { get; set; }
  5. public DateTime LoginDate { get; set; }
  6. public TimeSpan LoginTime { get; set; }
  7. public DateTime LogoutDate { get; set; }
  8. public TimeSpan LogoutTime { get; set; }
  9. public string? ComputerName { get; set; }
  10. public string? LogonServer { get; set; }
  11. }

When I make Select All queryies

  1. var attendanceData = dbContext.tblAttendances
  2. .Select(a =&gt; new AttendanceData
  3. {
  4. UserName = a.UserName ?? &quot;&quot;,
  5. LoginDate = a.LoginDate,
  6. LoginTime = a.LoginTime,
  7. LogoutDate = a.LogoutDate,
  8. LogoutTime = a.LogoutTime,
  9. ComputerName = a.ComputerName ?? &quot;&quot;,
  10. LogonServer = a.LogonServer ?? &quot;&quot;
  11. });

the query returns correct results.

But when I try to filter the data I receive a "Data is Null" error.

I have modified my code to use a simple FromSqlRaw statement

  1. string query = @$&quot;Select * from tblAttendance &quot;;
  2. if (!string.IsNullOrEmpty(userName))
  3. {
  4. if (query.IndexOf(&quot;where&quot;) &lt; 0) query += &quot;where &quot;;
  5. query +=$&quot;UserName = &#39;{userName}&#39; &quot;;
  6. }
  7. if (!string.IsNullOrEmpty(computerName))
  8. {
  9. if (query.IndexOf(&quot;where&quot;) &lt; 0) query += &quot;where &quot;; else query += &quot;AND &quot;;
  10. query += $&quot;ComputerName = &#39;{computerName}&#39; &quot;;
  11. }
  12. if (loginDate.Ticks&gt;0)
  13. {
  14. var year=loginDate.Year; var month=loginDate.Month;
  15. if (query.IndexOf(&quot;where&quot;) &lt; 0) query += &quot;where &quot;; else query += &quot;AND &quot;;
  16. query += $&quot; DATEPART(year, loginDate) = {year} and DATEPART(month, loginDate) = {month}&quot;;
  17. }
  18. var results=dbContext.tblAttendances.FromSqlRaw(query).ToList();

which also gives the Data is Null error, although when run through server explorer it returns correct results.

My data context builder for this table is

  1. modelBuilder.Entity&lt;tblAttendance&gt;(entity =&gt;
  2. {
  3. entity.HasKey(e =&gt; e.id).HasName(&quot;PK_tblAttendanceScript&quot;);
  4. entity.ToTable(&quot;tblAttendance&quot;);
  5. entity.HasIndex(e =&gt; new { e.UserName, e.LoginDate }, &quot;UserNames_tblAttendanceScript&quot;);
  6. entity.Property(e =&gt; e.ComputerName).HasMaxLength(50);
  7. entity.Property(e =&gt; e.LoginDate).HasColumnType(&quot;date&quot;);
  8. entity.Property(e =&gt; e.LogonServer).HasMaxLength(50);
  9. entity.Property(e =&gt; e.LogoutDate).HasColumnType(&quot;date&quot;);
  10. entity.Property(e =&gt; e.UserName).HasMaxLength(50);
  11. });

Being new to EF (and Core) I am uncertain now where to even look. A general (admittedly brief) search online has revealed nothing that I can apply.

******************** Edit adding sample data ***************************
Here is a sample of the results I receive from the database running the query directly:

  1. 749405 aanderso 2023-06-29 08:58:48.0000000 2023-06-29 16:05:17.0000000 MCA-TS24VM 192.168.100.90
  2. 749423 aanderso 2023-06-29 15:00:20.0000000 2023-06-29 16:05:17.0000000 MCA-TS25VM 192.168.100.97
  3. 762067 aanderso 2023-06-29 08:58:48.0000000 2023-06-29 16:05:17.0000000 MCA-TS24VM 192.168.100.90
  4. 724075 abala 2023-06-01 07:34:42.0000000 2023-06-01 NULL MCA-TS22VM 192.168.100.79
  5. 734462 abala 2023-06-01 07:34:42.0000000 2023-06-01 18:23:26.0000000 MCA-TS22VM 192.168.100.79
  6. 747249 abala 2023-06-01 07:34:42.0000000 2023-06-01 18:23:26.0000000 MCA-TS22VM 192.168.100.79
  7. 759911 abala 2023-06-01 07:34:42.0000000 2023-06-01 18:23:26.0000000 MCA-TS22VM 192.168.100.79

The query being used is Select * from tblAttendance where DATEPART(year, loginDate) = 2023 and DATEPART(month, loginDate) = 6

答案1

得分: 1

在EF表模型定义中:

  1. public partial class tblAttendance
  2. {
  3. public long id { get; set; }
  4. public string? UserName { get; set; }
  5. public DateTime LoginDate { get; set; }
  6. public TimeSpan LoginTime { get; set; }
  7. public DateTime LogoutDate { get; set; }
  8. public TimeSpan? LogoutTime { get; set; }
  9. public string? ComputerName { get; set; }
  10. public string? LogonServer { get; set; }
  11. }

LogoutTime 的定义为必需(没有 '?')。 这告诉 EF 不能有空值。 然而,在数据库中,允许有空的注销时间。 这是导致查询出现问题的原因。 修改为 public TimeSpan? LogoutTime { get; set; } 后,一切都按设计工作了。

英文:

In the EF table model definition

  1. public partial class tblAttendance
  2. {
  3. public long id { get; set; }
  4. public string? UserName { get; set; }
  5. public DateTime LoginDate { get; set; }
  6. public TimeSpan LoginTime { get; set; }
  7. public DateTime LogoutDate { get; set; }
  8. public TimeSpan LogoutTime { get; set; }
  9. public string? ComputerName { get; set; }
  10. public string? LogonServer { get; set; }
  11. }

LogoutTime public TimeSpan LogoutTime { get; set; } is defined as required (no '?'). This is telling EF that there cannot be a Null value. In the database, however, it is possible to have a Null logout time. This is what was breaking the query. Modified this to public TimeSpan? LogoutTime { get; set; } and everything worked as it was designed to.

huangapple
  • 本文由 发表于 2023年7月13日 09:37:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定