代码之家  ›  专栏  ›  技术社区  ›  Mark Perry

EF ASNotTracking导致错误。“System.DateTime”类型的表达式不能用于分配给“System.Nullable”类型的“1[System.DateTime]”

  •  3
  • Mark Perry  · 技术社区  · 6 年前

    今天,我决定将我的一个旧的Web项目从ASP.NET核心1.1升级到2.1,一切进展顺利,直到我尝试从数据库中检索LocalEvent的列表。我使用了.asnotracking,因为除了排序/过滤之外,我不需要对对象进行任何更改。

    这是错误:

    “System.DateTime”类型的表达式不能用于赋值给 类型“system.nullable`1[system.datetime]”

    这是我遇到问题的代码:

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

    这是本地事件模型:

    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; }
    }
    

    现在,我只是简单地取消了跟踪,以使事情再次工作。我做错什么了吗?

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

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

    希望有人能对这个问题有所了解!事先谢谢!

    1 回复  |  直到 6 年前
        1
  •  3
  •   Ivan Stoev    6 年前

    #12215: Exception when loading entity containing a primitive property with matching System.Nullable backing field

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // ...
    
        modelBuilder.Entity<LocalEvent>()
            .Property(e => e.DateCreated)
            .UsePropertyAccessMode(PropertyAccessMode.Property);
    
        // ...
    }
    
    推荐文章