是否可以使用仿照 N 层设计基于关键字查询 AspNetUsers 表:
Implementing a generic data access layer using Entity Framework
我在 DAL 中创建了以下接口(interface):
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 模型而不影响数据库的 Code First 部分?
最佳答案
我终于找到答案了。它实际上就在盯着我的脸。
只需使用以下代码即可获取用户对象:
var context = new ApplicationDbContext();
var user = context.Users.{Methods and Lambda expressions};
例如,假设您需要现有KNOWN事件用户的用户名,请使用User.Identity.Name
。并获取事件用户的 AspNetUsers 数据库记录:
var context = new ApplicationDbContext();
var user = context.Users.SingleOrDefault(u => u.UserName == User.Identity.Name);
关于c# - 在 Code First MVC 应用程序中查询 AspNet Identity 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34434604/