我在缺少引用的情况下调用 .Net Core 中的上下文时出错
Errors wih my call to context in .Net Core with missing reference
我似乎无法理解为什么我的方法在最底层有错误。
我有下面这两种方法,效果很好。
public async Task<Claim> GetClaim(string id)
{
var query = _context.Claims.AsQueryable();
var claim = await query.FirstOrDefaultAsync(c => c.ClaimNumber == id);
return claim;
}
public async Task<IEnumerable<Message>> GetMessageThread(int userId, int recipientId)
{
var messages = await _context.Messages
.Include(u => u.Sender).ThenInclude(p => p.Photos)
.Include(u => u.Recipient).ThenInclude(p => p.Photos)
.Where(m => m.RecipientId == userId && m.RecipientDeleted == false
&& m.SenderId == recipientId
|| m.RecipientId == recipientId && m.SenderId == userId
&& m.SenderDeleted == false)
.OrderByDescending(m => m.MessageSent)
.ToListAsync();
return messages;
}
但是用这种方法我在第一行下得到两个红色错误,最后
'DbSet' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'DbSet' could be found
和
Task> results
Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists
方法在这里
public async Task<IEnumerable<Claim>> GetClaims(GetClaimsDto claimParams)
{
var claims = await _context.Claims;
if (claimParams.MatterNumber != null) {
claims = claims.Where(c => c.MatterNumber == claimParams.MatterNumber);
}
if (claimParams.FirstName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.FirstName, "%" + claimParams.FirstName + "%"));
}
if (claimParams.LastName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.LastName, "%" + claimParams.LastName + "%"));
}
var results = claims.OrderByDescending(m => m.MatterNumber).ToListAsync();
return results;
}
这是我的声明实体:
public class Claim
{
public int Id { get; set; }
public string ClaimNumber { get; set; }
public string MatterNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Data { get; set; }
[NotMapped]
public string[] Errors { get; set; }
[NotMapped]
public List<string> SearchResults { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime EditedDate { get; set; }
public string EditedBy { get; set; }
}
这里是错误
_context.Claims
不是异步的,因此您不需要等待它。
.ToListAsync()
是异步的,因此您需要等待它。
试试这个。
public async Task<IEnumerable<Claim>> GetClaims(GetClaimsDto claimParams)
{
var claims = _context.Claims;
if (claimParams.MatterNumber != null) {
claims = claims.Where(c => c.MatterNumber == claimParams.MatterNumber);
}
if (claimParams.FirstName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.FirstName, "%" + claimParams.FirstName + "%"));
}
if (claimParams.LastName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.LastName, "%" + claimParams.LastName + "%"));
}
var results = await claims.OrderByDescending(m => m.MatterNumber).ToListAsync();
return results;
}
我似乎无法理解为什么我的方法在最底层有错误。
我有下面这两种方法,效果很好。
public async Task<Claim> GetClaim(string id)
{
var query = _context.Claims.AsQueryable();
var claim = await query.FirstOrDefaultAsync(c => c.ClaimNumber == id);
return claim;
}
public async Task<IEnumerable<Message>> GetMessageThread(int userId, int recipientId)
{
var messages = await _context.Messages
.Include(u => u.Sender).ThenInclude(p => p.Photos)
.Include(u => u.Recipient).ThenInclude(p => p.Photos)
.Where(m => m.RecipientId == userId && m.RecipientDeleted == false
&& m.SenderId == recipientId
|| m.RecipientId == recipientId && m.SenderId == userId
&& m.SenderDeleted == false)
.OrderByDescending(m => m.MessageSent)
.ToListAsync();
return messages;
}
但是用这种方法我在第一行下得到两个红色错误,最后
'DbSet' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'DbSet' could be found
和
Task> results Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists
方法在这里
public async Task<IEnumerable<Claim>> GetClaims(GetClaimsDto claimParams)
{
var claims = await _context.Claims;
if (claimParams.MatterNumber != null) {
claims = claims.Where(c => c.MatterNumber == claimParams.MatterNumber);
}
if (claimParams.FirstName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.FirstName, "%" + claimParams.FirstName + "%"));
}
if (claimParams.LastName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.LastName, "%" + claimParams.LastName + "%"));
}
var results = claims.OrderByDescending(m => m.MatterNumber).ToListAsync();
return results;
}
这是我的声明实体:
public class Claim
{
public int Id { get; set; }
public string ClaimNumber { get; set; }
public string MatterNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Data { get; set; }
[NotMapped]
public string[] Errors { get; set; }
[NotMapped]
public List<string> SearchResults { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime EditedDate { get; set; }
public string EditedBy { get; set; }
}
这里是错误
_context.Claims
不是异步的,因此您不需要等待它。
.ToListAsync()
是异步的,因此您需要等待它。
试试这个。
public async Task<IEnumerable<Claim>> GetClaims(GetClaimsDto claimParams)
{
var claims = _context.Claims;
if (claimParams.MatterNumber != null) {
claims = claims.Where(c => c.MatterNumber == claimParams.MatterNumber);
}
if (claimParams.FirstName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.FirstName, "%" + claimParams.FirstName + "%"));
}
if (claimParams.LastName != String.Empty) {
claims = claims.Where(c => EF.Functions.Like(c.LastName, "%" + claimParams.LastName + "%"));
}
var results = await claims.OrderByDescending(m => m.MatterNumber).ToListAsync();
return results;
}