您好,我正在Asp.net MVC CORE项目中使用根据here改编的存储库模式。当我使用GetAll()
方法拨打数据库的电话时如果我尝试使用
T GetSingle(object id);
T GetSingle(Expression<Func<T, bool>> predicate);
喜欢这个var model = _repository.GetSingle(x => x.Id == id);
或类似[var model = _repository.GetSingle(id);
模型返回Null。
如果我从@model IEnumerable<ApplicationUser>
更改而在GetAll()
上可以正常工作,则在Razor视图中>]
对此@model ApplicationUser
,我为空。我的控制器是这样的:
[HttpGet] public IActionResult Index(string id) { //var model = _repository.GetAll(); var model = _repository.GetSingle(x => x.Id == id); //var model = _repository.GetSingle(id); if(model == null) { return NotFound(); } return View(model); }
如果有帮助,这里是存储库的声明:
public class EntityBaseRepository<T> : IRepository<T> where T : class, new()
我想知道为什么数据库使用这两种方法返回Null?
这些是方法
public T GetSingle(object id)
{
return _context.Set<T>().Find(id);
}
public T GetSingle(Expression<Func<T, bool>> predicate)
{
return _context.Set<T>().FirstOrDefault(predicate);
}
public IEnumerable<T> GetAll()
{
return _context.Set<T>().AsEnumerable();
}
您好,我正在Asp.net MVC CORE项目中使用从此处改编的存储库模式。如果我尝试使用T GetSingle(object ...
请尝试这样重写您的GetSingle方法: