拥有一个IList<Guid>
,我希望从上下文中找到所有匹配项,然后包含一些相关的表,其中不包括过时的数据。由于数据的大小,我尝试使用 EF Plus IncludeFilter,以避免将所有内容加载到内存并在那里执行过滤。当我调用ToListAsync()
查询时会出现问题。IncludeFilter (据我所知)然后引发System.MissingMethodException : Cannot create an instance of an interface
异常。
该项目在 .NET Core 2.2 中完成,我使用 Z.EntityFramework.Plus.EFCore 2.0.7
示例项目
此示例重现了该问题:https ://dotnetfiddle.net/MYukHp
数据结构
数据库结构以 Facts 表为中心,该表包含一个不可变的 Guid,用于标识每个条目。所有其他表中的条目都链接到此 guid 以绑定到单个条目。其他表包含一个DateTime ValidTo
用于跟踪更改。没有条目被更新或删除。相反,在更改时使用 生成新条目ValidTo = DateTime.MaxValue
,并且正在更新的条目将其ValidTo
设置为DateTime.Now
。这可确保所有更改都保留在历史上。
随着时间的推移,绝大多数数据将是历史数据,因此我们可以在 SQL 查询中将其过滤掉是至关重要的。
Fact-table 的数据结构如下:
public class FactModel
{
public Guid FactId { get; set; }
public DateTime ValidFrom { get; set; }
public DateTime ValidTo { get; set; }
// Navigation properties
public IEnumerable<PersonModel> Persons { get; set; }
// Repeat for all other tables
}
所有其他表都继承自 ModelBase,将它们链接到 Fact 表。
public class ModelBase
{
public Guid FactId { get; set; } // Link to the Fact
public FactModel Fact { get; set; } // Navigation property
public DateTime ValidFrom { get; set; }
public DateTime ValidTo { get; set; } // ValidTo == DateTime.MaxValue -> active record
}
人员和患者的示例表
public class PersonModel : ModelBase
{
public Guid PersonId { get; set; } // Key - A new is created on every update
public string FirstName { get; set; } // data
public string LastName { get; set; } // data
}
public class PatientModel : ModelBase
{
public Guid PatientId { get; set; } // Key - A new is created on every update
public Guid ActiveCompanyId { get; set; } // Data
public int HealthInsuranceGroup { get; set; } // Data
public PatientStatusType Status { get; set; } // Data
}
将参数更改为 IQueryable 会产生一个新错误:
System.InvalidCastException : Unable to cast object of type System.Guid' to type 'System.String'
调用序列
调用序列相当复杂,但我们从声明调用参数开始简化IQueryable<FactModel> facts
。这是通过仅添加用户登录公司的患者来过滤的。然后应用搜索词。最后,在调用AssignDataToPatientByFactId
.
// Performing a search for an Address
IQueryable<FactModel> facts = null;
facts = _context.Facts.AsNoTracking().Where(p => p.Patients.Any(a => a.ActiveCompanyId == _identificationHandler.Identification.CompanyId));
facts = facts.AsNoTracking().Where(p => p.Addresses.Where(z => z.ValidTo == DateTime.MaxValue).Any(q => q.Street.Any() && q.Street.StartsWith(searchDTO.SearchString)));
return await AssignDataToPatient(facts.Select(x => x.FactId).ToList()), cancel);
public async Task<List<FactModel>> AssignDataToPatientByFactId(IList<Guid> factIds, CancellationToken cancel)
{
return await _context.Facts.Where(x => factIds.Contains(x.FactId))
.IncludeFilter(x => x.Patients.Where(c => c.ValidTo == DateTime.MaxValue))
.IncludeFilter(x => x.Persons.Where(c => c.ValidTo == DateTime.MaxValue))
.IncludeFilter(x => x.Communications.Where(c => c.ValidTo == DateTime.MaxValue))
.IncludeFilter(x => x.Addresses.Where(c => c.ValidTo == DateTime.MaxValue))
.ToListAsync(cancel);
}
所以AssignDataToPatientByFactId
获取一个 guid 列表,在 Facts 表中找到所有匹配项,然后添加ValidTo
时间戳为 Max 的其他表中的条目。因此,不应包括所有其他条目。
将代码分成几个语句表明 IncludeFilter 似乎正在工作,但调用 ToListAsync 会产生错误。