今天,我决定将我的一个旧 Web 项目从 Asp.net core 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();
这是 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,我删除了与日期有关的任何内容,最初只是删除了可为空的日期,然后我意识到对管理员列表的类似调用(一个完整的跟踪列表) 的唯一区别是 AsNoTracking,那时(测试后)我决定在这里提问。
我的想法是,如果我无意修改、删除等,我应该避免跟踪以提高性能!?
希望有人可以对这个问题有所了解!提前致谢!