调用存储库模式方法在Asp.Net Mvc Core中返回null

问题描述 投票:0回答:1

您好,我正在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 ...

c# asp.net-mvc asp.net-core entity-framework-core repository-pattern
1个回答
1
投票

请尝试这样重写您的GetSingle方法:

© www.soinside.com 2019 - 2024. All rights reserved.