是否可以使用以下建模的N-Tier设计基于关键字查询AspNetUsers表:
Implementing a generic data access layer using Entity Framework
我在DAL中创建了以下接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Library.Model;
namespace Library.DataAccessLayer
{
...
public interface IAspNetUserRepository : IGenericDataRepository<ApplicationUser>
{
}
public class AspNetUserRepository : GenericDataRepository<ApplicationUser>, IAspNetUserRepository
{
}
}
以及以下BLL条目:
using System;
using System.Collections.Generic;
using System.Linq;
using Library.DataAccessLayer;
using Library.Model;
namespace Library.BusinessLogicLayer
{
public interface IBusinessLogicLayer_AspNetUser
{
ApplicationUser GetAspNetUserByAspNetUserID(string _UserID);
}
public class BusinessLogicLayer_AspNetUser : IBusinessLogicLayer_AspNetUser
{
private readonly IAspNetUserRepository _AspNetUserRepository;
public BusinessLogicLayer_AspNetUser()
{
_AspNetUserRepository = new AspNetUserRepository();
}
public BusinessLogicLayer_AspNetUser(IAspNetUserRepository AspNetUserRepository)
{
_AspNetUserRepository = AspNetUserRepository;
}
public ApplicationUser GetAspNetUserByAspNetUserID(string _UserID)
{
return _AspNetUserRepository.GetSingle(u => u.Id.Equals(_UserID));
}
}
}
现在在BLL文件中,唯一的Lambda表达式不包含Td,因此会出错。
什么是要使用的正确模型。我能找到的唯一信息是ApplicationUser继承了Identity User。
需要做什么或更改什么才能将这些字段添加到ApplicationUser模型中,而又不影响数据库的“代码优先”部分?
最佳答案
我终于找到了答案。它实际上正盯着我。
只需使用以下代码即可获取用户对象:
var context = new ApplicationDbContext();
var user = context.Users.{Methods and Lambda expressions};
例如,假设您需要现有的已知活动用户的UserName,请使用
User.Identity.Name
。并获取活动用户的AspNetUsers数据库记录:var context = new ApplicationDbContext();
var user = context.Users.SingleOrDefault(u => u.UserName == User.Identity.Name);