gpt4 book ai didi

c# - 为什么我的 EF 似乎从正在运行的 SQL View 中返回重复的行?

转载 作者:行者123 更新时间:2023-12-03 18:40:59 24 4
gpt4 key购买 nike

我查过这个问题,但我发现没有任何东西对我有用。我在 SQL 中创建了一个 View ,当您在 Management Studio 中运行它时该 View 有效。当从我的 MVC 应用程序访问 View 时,EF 返回相同的行而不是具有不同数据的行。

表:汽车

  • [Id]
  • [注册]
  • [制作]
  • [模型]

  • 表:预订
  • [Id]
  • [预订开始日期]
  • [预订结束日期]
  • [CarId]

  • 查看:CarBookings
    SELECT  [C].[Id],
    [C].[Registration],
    [C].[Make],
    [C].[Model],
    [B].[BookingStartDate],
    [B].[BookingEndDate]

    FROM [Cars] AS C INNER JOIN [Bookings] AS B ON C.Id = B.CarId

    如果我在 SSMS 中运行查询,我会得到所有预期的结果,例如:
  • 第 1 辆车,2018 年 3 月 12 日预订
  • 第 1 辆车,2018 年 9 月 19 日预订

  • 当我从我的 MVC 应用程序访问相同的 View 时,我得到:
  • 第 1 辆车,2018 年 3 月 12 日预订
  • 第 1 辆车,2018 年 3 月 12 日预订

  • 在 Controller 上放置一个断点表明结果是相同的,所以它不是导致它的表示层。没有应用过滤器,也没有任何条件。

    我正在使用 KendoUI 并将我的结果返回到 Grid

    这是我用于获取数据的 Controller 代码:

    HomeController.cs
    public ActionResult GetBookings([DataSourceRequest] DataSourceRequest request)
    {
    var bookings = unitOfWork.BookingsRepository.Get();
    var result = bookings.ToDataSourceResult(request);
    return Json(result, JsonRequestBehavior.AllowGet);
    }

    我的应用程序使用通用存储库。我不确定它是否会导致问题,但值得一提。这是我的存储库中的 GET 方法。

    DAL/GenericRepository.cs
    public virtual IEnumerable<TEntity> Get(
    Expression<Func<TEntity, bool>> filter = null,
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    string includeProperties = "")
    {
    IQueryable<TEntity> query = dbSet;

    if (filter != null)
    {
    query = query.Where(filter);
    }

    foreach (var includeProperty in includeProperties.Split
    (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    {
    query = query.Include(includeProperty);
    }

    if (orderBy != null)
    {
    return orderBy(query).ToList();
    }
    else
    {
    return query.ToList();
    }
    }

    DAL/Context.cs
    public DbSet<Bookings> Bookings { get; set; }

    DAL/UnitOfWork.cs
     private GenericRepository<Bookings> bookingsRepository;
    public GenericRepository<Bookings> bookingsRepository
    {
    get
    {
    if (this.bookingsRepository == null)
    {
    this.bookingsRepository = new GenericRepository<Bookings>(context);
    }
    return bookingsRepository;
    }
    }

    实体类

    这是表示 View 并使用 [Table] 注释访问它的类。
    namespace MyProject.Models
    {
    [Table("CarBookings")]
    public class Bookings
    {
    //Car
    [Key]
    public int Id { get; set; }
    public string Registration { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }

    //Booking
    public DateTime BookingStartDate { get; set; }
    public DateTime BookingEndDateYearOfBuild { get; set; }
    }
    }

    当我搜索此问题的答案时,我读到该 View 没有 ID,因此 EF 尝试按唯一值对记录进行逻辑排序,这有时会导致问题(来源: https://www.itworld.com/article/2833108/development/linq-in--net-returning-duplicate-rows-from-a-working-sql-view--solved-.html)。

    我按照上面的文章调整了我的 View select 代码,但它对我不起作用;我仍然看到重复:
    SELECT ROW_NUMBER() OVER (ORDER BY Car.Id) AS NID, 
    Car.Id,
    Booking.BookingStartDate
    ... etc...

    FROM Cars AS Car INNER JOIN
    Booking AS Booking ON Car.Id = Booking.Car_Id

    最佳答案

    我做了更多的挖掘,除了上面提到的 [Key] View 之外,我发现的其他线程指出 .AsNoTracking() 作为潜在的解决方案。我对此进行了更多调查,并尝试在我的解决方案中实现这一点。
    这是与我的问题有关的评论之一:

    AsNoTracking() allows the "unique key per record" requirement in EF to be bypassed (not mentioned explicitly by other answers).

    This is extremely helpful when reading a View that does not support a unique key because perhaps some fields are nullable or the nature of the view is not logically indexable.

    For these cases the "key" can be set to any non-nullable column but then AsNoTracking() must be used with every query else records (duplicate by key) will be skipped


    资料来源: What difference does .AsNoTracking() make?
    在我的 GenericRepository.cs 中,我在 Get 方法上设置了这个值,我的网格上的结果现在是准确的,没有任何重复。
    这是我更改的代码:
    GenericRepository.cs
    public virtual IEnumerable<TEntity> Get(
    Expression<Func<TEntity, bool>> filter = null,
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    string includeProperties = "")
    {
    IQueryable<TEntity> query = dbSet.AsNoTracking();

    if (filter != null)
    {
    query = query.Where(filter);
    }

    foreach (var includeProperty in includeProperties.Split
    (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    {
    query = query.Include(includeProperty);
    }

    if (orderBy != null)
    {
    return orderBy(query).ToList();
    }
    else
    {
    return query.ToList();
    }
    }
    这种变化解决了我的问题。希望以后不会有任何不良影响:) 感谢所有花时间回复的人。

    关于c# - 为什么我的 EF 似乎从正在运行的 SQL View 中返回重复的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52075262/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com