EF AsNoTracking导致错误。表达式类型System.DateTime'不能用于分配类型' System.Nullable`1 [System.DateTime]'

时间:2018-06-09 16:48:59

标签: c# entity-framework entity-framework-core ef-core-2.1

今天我决定将我的旧网络项目从Asp.net核心1.1升级到2.1,所有这些都很顺利,直到我试图从数据库中检索LocalEvent列表。我正在使用.AsNotracking,因为我不需要对排序/过滤之外的对象进行任何更改。

这是错误:

  

表达类型&System;日期时间'不能用于作业   键入System.Nullable`1 [System.DateTime]'

这是我遇到问题的代码:

    var today = DateTime.Now;

    var events = await _context.LocalEvent.Where
    (x => x.ReoccurUntil > today || x.EventEnd > today).AsNoTracking().ToListAsync();

这是LocalEvent模型:

public class LocalEvent
{
    [NotMapped]
    private DateTime? dateCreated;

    [Key]
    public Guid Id { get; set; }

    [MaxLength(200)]
    [Display(Name = "Event Location")]
    [DataType(DataType.MultilineText)]
    public string Location { get; set; }

    [Display(Name = "Added By")]
    public string Author { get; set; }

    [Display(Name = "Contact")]
    public string EventContact { get; set; }

    [Display(Name = "Event Name")]
    public string EventName { get; set; }

    [DataType(DataType.MultilineText)]
    [Display(Name = "Details")]
    public string EventInfo { get; set; }

    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
    [Display(Name = "Cost")]
    public Decimal? EventCost { get; set; }

    //DateStuff
    [Display(Name = "Date Created")]
    public DateTime DateCreated
    {
        get { return dateCreated ?? DateTime.Now; }
        set { dateCreated = value; }
    }

    [Display(Name = "Event Start")]
    public DateTime EventStart { get; set; }


    [DateGreaterThan("EventStart")]
    [Display(Name = "Event End")]
    public DateTime EventEnd { get; set; }

    public DateTime ReoccurUntil { get; set; }

    [Display(Name = "Reoccurrence Type")]
    public TypeOfOccurrence OccurrenceType { get; set; }

    [Display(Name = "Which Occurence of Weekday")]
    public Nth NthReOccurence { get; set; }

    [Display(Name = "Weekday for Reoccurence")]
    public DayOfWeek ReoccurrenceDay { get; set; }

    [Display(Name = "Occurrence Rate")]
    public OccurrenceRate Occurrence { get; set; }
    public bool DoesReoccur { get; set; }

    [DataType(DataType.ImageUrl)]
    public string Image { get; set; }

    public string ImageDeletePath { get; set; }
    public string Slug { get; set; }
}

现在我只是删除了AsNoTracking,让事情再次发挥作用。难道我做错了什么?

这个错误非常奇怪,因为它提到了日期,所以花了一段时间追溯到AsNoTracking,我删除了与日期有关的任何内容,最初只是删除了可以考虑的日期,然后我实现了对管理列表的类似调用(a完整的跟踪列表)唯一的区别是AsNoTracking,以及那时(测试后)我决定在这里询问。

我的想法是,如果我无意修改,删除等,我应该避免跟踪以提高性能!?

希望有人能够对这个问题有所了解!提前谢谢!

1 个答案:

答案 0 :(得分:3)

它是由v2.1中引入的EF Core bug引起的。

#12215: Exception when loading entity containing a primitive property with matching System.Nullable backing field报告和跟踪。

未提供解决方法,修复了针对v2.2的安排。

我可以建议使用属性强制EF而不是支持字段:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // ...

    modelBuilder.Entity<LocalEvent>()
        .Property(e => e.DateCreated)
        .UsePropertyAccessMode(PropertyAccessMode.Property);

    // ...
}