【发布时间】:2018-02-28 15:34:47
【问题描述】:
我快被这个吓坏了,我正在使用存储库模式和 unityOfwork,如果我使用 db.context 带来一个列表工作正常如果我在存储库上使用相同的查询带来重复的值,这是我的代码。
这段代码带来了正确的数据
var myList = (from p in db.Products_Sizes
where p.productsID == 181 && p.ColorID == 200 && p.SizeID == 133
select new SelectListItem
{
Value = p.eyepower.ToString(),
Text = p.eyepower.ToString()
}).OrderBy(p => p.Value).ToList();
正确的数据是
- 1.00
- 1.50
- 2.00
- 2.50
现在,如果我使用此代码。
var myList = _unitOfWork.ProductsSizes.Find(a => a.productsID == 181 && a.ColorID == 200 && a.SizeID == 133).ToList()
.Select(u => new SelectListItem
{
Value = u.eyepower.ToString(),
Text = u.eyepower.ToString()
});
结果
- 1.00
- 1.00
- 1.00
- 1.00
或任何其他重复
这里是 Find `IEnumerable Find(Expression> predicate);在界面中
我知道数据是正确的,因为我在 sql server 中运行了一个查询并且数据很好
我不知道为什么,有什么想法吗? 这是我的整个结构 基础接口
public interface IRepositoryBase<T> where T : class
{IEnumerable<T> Find(Expression<Func<T, bool>> predicate);}
基础仓库
public class RepositoryBase<T> : IRepositoryBase<T> where T : class
{protected readonly DbContext Context;
public RepositoryBase(DbContext context)
{
Context = context;
}
public T Get(int id)
{
return Context.Set<T>().Find(id);
}
public IEnumerable<T> GetAll()
{
return Context.Set<T>().ToList();
}
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return Context.Set<T>().Where(predicate);
}}
productsize 仓库是这样的
public interface IProductsSizesRepository : IRepositoryBase<Products_Sizes>
{
}
unityOfWork 接口
public interface IUnitOfWork : IDisposable
{
IOrdersRepository ProductSizes{ get; }}
unityOfwork 类
public class UnitOfWork : IUnitOfWork
{
public readonly EFDbContext _dbContext;
public UnitOfWork(EFDbContext dbContext)
{
_dbContext = dbContext;
ProductsSizes = new ProductsSizesRepository(_dbContext);
}
public IProductsSizesRepository ProductsSizes { get; private set; }}
最后是我遇到此问题的控制器,您可以看到我正在使用 ninject 在控制器中注入 IUnityOfWork 以访问存储库
public class SelectProductController : Controller{private readonly IUnitOfWork _unitOfWork;
public SelectProductController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}}
【问题讨论】:
-
而不是 Find,使用 Where
-
已经做了,结果一样
-
会不会是
_unitOfWork.ProductsSizes不等于db.Products_Sizes? -
使用
((System.Data.Entity.Core.Objects.ObjectQuery)query) .ToTraceString();检查生成的SQL -
不是'public IProductsSizesRepository ProductsSizes { get;私人套装; }' 从这里
标签: c# entity-framework linq asp.net-mvc-5