我使用实体框架 4.3.1.0
SQL Server 2008 速成版
我有意见
SELECT dbo.Dealers.Name AS DealerName,
dbo.Dealers.LogoImage,
dbo.DealersProducts.Price,
dbo.DealersProducts.StatusType,
dbo.Products.Description,
dbo.Products.Name,
dbo.DealersProducts.DealerId,
dbo.Products.Id
FROM dbo.Dealers
INNER JOIN
dbo.DealersProducts
ON dbo.Dealers.Id = dbo.DealersProducts.DealerId
INNER JOIN
dbo.Products
ON dbo.DealersProducts.ProductId = dbo.Products.Id
我有实体
public class DealerViews : BasePersistentEntity
{
public int DealerId { get; set; }
public string DealerName { get; set; }
public string LogoImage { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int StatusType { get; set; }
}
此视图用于退货经销商有要求的产品。当我在 SQL 管理器中请求 SQL 查询时,我选择了正确的结果,但在实体框架中我得到了奇怪的结果。例子
SQL 结果
经销商 1
经销商 2
实体框架结果
经销商 1
经销商 1
控制器中的代码
public ActionResult ShowProductDealers(int id)
{
var dealer = this.dealerService.GetDealerByProductId(id);
return this.PartialView("ShowProductDealers", dealer);
}
服务中的代码
public IEnumerable<DealerViews> GetDealerByProductId(int id)
{
return this.repositoryViews.All.Where(item => item.Id == id);
}
存储库中的代码
public class SGNRepository<T> where T : BasePersistentEntity
{
public readonly SGNContext<T> Context = new SGNContext<T>();
public IQueryable<T> All
{
get { return this.Context.Table; }
}
public IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = this.Context.Table;
return includeProperties.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
}
public T Find(int id)
{
return this.Context.Table.Find(id);
}
public void InsertOrUpdate(T item)
{
if (item.Id == default(int))
{
// New entity
this.Context.Table.Add(item);
}
else
{
// Existing entity
this.Context.Entry(item).State = EntityState.Modified;
}
this.Save();
}
public void Delete(int id)
{
var item = this.Context.Table.Find(id);
this.Context.Table.Remove(item);
this.Save();
}
private void Save()
{
this.Context.SaveChanges();
}