为ASP.NET core Web API应用程序实现服务并获得持久错误。
接口:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using API.Data.Dtos.InviteDto;
using API.Domain;
namespace API.Services
{
public interface IInviteService
{
Task<UserInvite> GetUserInvite(Guid sourceUserId, Guid invitedUserId);
Task<IEnumerable<InviteDto>> GetUserInvites(string predicate, Guid userId);
Task<User> GetUserWithInvites(Guid userId);
}
}
服务:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.Data.Dtos.InviteDto;
using API.Domain;
using Microsoft.EntityFrameworkCore;
namespace API.Services
{
public class InviteService : IInviteService
{
private readonly DataContext _context;
public InviteService(DataContext context)
{
_context = context;
}
public async Task<UserInvite> GetUserInvite(Guid sourceUserId, Guid invitedUserId)
{
return await _context.Invites.FindAsync(sourceUserId, invitedUserId);
}
public async Task<User> GetUserWithInvites(Guid userId)
{
return await _context.Users.Include(x => x.InvitedUsers).FirstOrDefaultAsync(x => x.Id == userId);
}
public async Task<IEnumerable<InviteDto>> GetUserInvites(string predicate, Guid userId)
{
var users = _context.Users.OrderBy(u => u.UserName).AsQueryable();
var invites = _context.Invites.AsQueryable();
if (predicate == "invited")
{
invites = invites.Where(invite => invite.SourceUserId == userId);
users = invites.Select(invite => invite.InvitedUser);
}
if (predicate == "invitedBy")
{
invites = invites.Where(invite => invite.InvitedUserId == userId);
users = invites.Select(invite => invite.SourceUser);
}
return await users.Select(user => new InviteDto
{
Username = user.UserName,
City = user.City
}).ToListAsync();
}
}
}
问题是VS中的一个错误。我重新启动,所有的东西都编译好了。谢谢所有帮忙的人。