【问题标题】:I get a an error when the Date is null but cannot understand why and how to fix it当 Date 为 null 但不明白为什么以及如何修复它时,我收到一个错误
【发布时间】:2020-07-11 01:33:19
【问题描述】:

这让我发疯:

            using (var context = new aContext())
            {
                _Paz = context.TPaz
                .Where(a => a.CodPaz == _PazV.CodPaz)
                .FirstOrDefault();
            }

我有 2 行不同的行,第一行一切都很完美。 在第二个我得到错误

这里是定义:

[Table("tPAZ")]
public partial class TPaz
{
    public TPaz()
    {
        TApp = new HashSet<TApp>();
        TApr = new HashSet<TApr>();
        TAsl = new HashSet<TAsl>();
        TCad = new HashSet<TCad>();
        TDia = new HashSet<TDia>();
        TEos = new HashSet<TEos>();
        TGen = new HashSet<TGen>();
        TImg = new HashSet<TImg>();
        TInt = new HashSet<TInt>();
        TIst = new HashSet<TIst>();
        TPlp = new HashSet<TPlp>();
    }

    [Key]
    [Column("COD_PAZ")]
    public int CodPaz { get; set; }
    [Required]
    [Column("COGNOME")]
    [StringLength(30)]
    public string Cognome { get; set; }
    [Required]
    [Column("NOME")]
    [StringLength(30)]
    public string Nome { get; set; }
    [Column("INDIR")]
    [StringLength(30)]
    public string Indir { get; set; }
    [Column("CITTA")]
    [StringLength(20)]
    public string Citta { get; set; }
    [Column("PROV")]
    [StringLength(2)]
    public string Prov { get; set; }
    [Column("CAP")]
    [StringLength(5)]
    public string Cap { get; set; }
    [Column("NAZIONE")]
    [StringLength(15)]
    public string Nazione { get; set; }
    [Column("NATOA")]
    [StringLength(20)]
    public string Natoa { get; set; }
    [Column("NATOP")]
    [StringLength(2)]
    public string Natop { get; set; }
    [Column("NATON")]
    [StringLength(15)]
    public string Naton { get; set; }
    [Column("NATODATA")]
    //        [StringLength(2048)]
    //        public string Natodata { get; set; }
    public DateTime Natodata { get; set; }
    [Column("TELEF1")]
    [StringLength(20)]
    public string Telef1 { get; set; }
    [Column("TELEF2")]
    [StringLength(20)]
    public string Telef2 { get; set; }
    [Column("FAX")]
    [StringLength(20)]
    public string Fax { get; set; }
    [Column("PRES_DA")]
    [StringLength(15)]
    public string PresDa { get; set; }
    [Column("COD_FIS")]
    [StringLength(16)]
    public string CodFis { get; set; }
    public short Status { get; set; }
    [Required]
    [StringLength(30)]
    public string Clinica { get; set; }
    [Required]
    [StringLength(15)]
    public string Assicurazione { get; set; }

    [NotMapped]
    public string NatodataS
    {
        get
        {
            if ((Natodata == null) || (DBNull.Value.Equals(Natodata)))
            //if (Natodata == null) 
            {
                return "";
            }
            else
            {
                return Natodata.ToString(GetCultureHelper.GetCurrentCulture().DateTimeFormat.ShortDatePattern);
            }
        }
    }

    [ForeignKey(nameof(Assicurazione))]
    [InverseProperty(nameof(TAssicurazioni.TPaz))]
    public virtual TAssicurazioni AssicurazioneNavigation { get; set; }
    [ForeignKey(nameof(Clinica))]
    [InverseProperty(nameof(TCliniche.TPaz))]
    public virtual TCliniche ClinicaNavigation { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TApp> TApp { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TApr> TApr { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TAsl> TAsl { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TCad> TCad { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TDia> TDia { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TEos> TEos { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TGen> TGen { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TImg> TImg { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TInt> TInt { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TIst> TIst { get; set; }
    [InverseProperty("CodPazNavigation")]
    public virtual ICollection<TPlp> TPlp { get; set; }
}

在上下文中:

    public virtual DbSet<TPaz> TPaz { get; set; }

这是数据:

COD_PAZ COGNOME NOME    INDIR   CITTA   PROV    CAP     NAZIONE NATOA   NATOP   NATON   NATODATA                    TELEF1  TELEF2  FAX     PRES_DA COD_FIS Status  Clinica Assicurazione
6       AAAA    BBB  v. G.      NULL    Mo      NULL    NULL    (Va)    NULL    NULL    1940-06-08 00:00:00.0000000 1234    3456    NULL    abcde   NULL    6       .       .
2895    aaaaa   pippo   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL                        NULL    NULL    NULL    NULL    NULL    99      .       .

第二行报错:

System.Data.SqlTypes.SqlNullValueException
  HResult=0x80131931
  Message=Data is Null. This method or property cannot be called on Null values.
  Source=Microsoft.Data.SqlClient
  StackTrace:
   at Microsoft.Data.SqlClient.SqlBuffer.get_DateTime()
   at Microsoft.Data.SqlClient.SqlDataReader.GetDateTime(Int32 i)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at TitoDoc_A.Models.MTreeViewPaz.get_Paz() in E:\Users\Massimo\OneDrive\Development\Source\TitoDoc_A\TitoDoc_A\Models\MTreeViewPaz.cs:line 57
   at TitoDoc_A.Views.TitoDocPage.TitoDocPage_obj86_Bindings.Update_(MTreeViewPaz obj, Int32 phase) in E:\Users\Massimo\OneDrive\Development\Source\TitoDoc_A\TitoDoc_A\obj\x86\Debug\Views\TitoDocPage.g.cs:line 3589
   at TitoDoc_A.Views.TitoDocPage.TitoDocPage_obj86_Bindings.Update() in E:\Users\Massimo\OneDrive\Development\Source\TitoDoc_A\TitoDoc_A\obj\x86\Debug\Views\TitoDocPage.g.cs:line 3558
   at TitoDoc_A.Views.TitoDocPage.TitoDocPage_obj86_Bindings.DataContextChangedHandler(FrameworkElement sender, DataContextChangedEventArgs args) in E:\Users\Massimo\OneDrive\Development\Source\TitoDoc_A\TitoDoc_A\obj\x86\Debug\Views\TitoDocPage.g.cs:line 3498

  This exception was originally thrown at this call stack:
    [External Code]
    TitoDoc_A.Models.MTreeViewPaz.Paz.get() in MTreeViewPaz.cs
    [External Code]

我已经检查过并且是由于 NATOD(日期时间)为 NULL,但我怎样才能避免错误?

【问题讨论】:

  • 如果模式中的值允许为null,那么您需要更改模型以反映这一点。示例:DateTime?Nullable&lt;DateTime&gt; 而不是 DateTime。如果您的架构不允许这样做,那么您需要修复现有数据,然后修复架构,以便该列为 NOT NULL
  • 完美谢谢!!这让我疯狂了 2 天......

标签: sql-server linq null entity-framework-core


【解决方案1】:

数据库中的字段允许为null,所以我通过使模型中的字段也可以为空来修复它。

[Column("NATODATA")]
public DateTime? Natodata { get; set; }

使用DateTime? 有效。

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    相关资源
    最近更新 更多