c# - 为什么我的 KendoUI 网格在我的 ASP.NET Core 应用程序中显示重复记录?

标签 c# sql asp.net-core kendo-ui

我在我的 ASP.NET Core 应用程序中使用 KendoUI 网格,当我将数据返回到我的网格时,它会显示相同的记录两次。网格从我的存储库获取数据,带注释的实体查找名为 ViewVesselCrane 的 View 。它应该返回与称为 VesselId 的特定 ID 匹配的数据行,但它似乎做的是返回正确数量的结果,但它们都是相同的数据。

首先,这是我的观点:

ViewVesselCrane.cs

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MyCompany.Data.Views
{
    [Table("ViewVesselCrane")]
    public class ViewVesselCrane
    {
        [Key]

        public int DeckEquipmentId { get; set; }
        public int? VesselId { get; set; }
        public int? CraneTypeId { get; set; }
        public int? NumberOfCranes { get; set; }
        public string TypeName { get; set; }
        public int? SWL { get; set; }

    }
}

服务/CraneViewService.cs

using MyCompany.Data.Views;
using MyCompany.Repo;
using System;
using System.Collections.Generic;
using System.Text;

namespace MyCompany.Services.Views
{
    public class CraneViewService : ICraneViewService
    {
        private IRepository<ViewVesselCrane> viewCraneRepository;

        public CraneViewService(IRepository<ViewVesselCrane> viewCraneRepository)
        {
            this.viewCraneRepository = viewCraneRepository;
        }
        public IEnumerable<ViewVesselCrane> GetCranes()
        {
            return viewCraneRepository.GetAll();
        }
        public ViewVesselCrane GetCrane(int id)
        {
            return viewCraneRepository.Get(id);
        }
        public void InsertCrane(ViewVesselCrane crane)
        {
            viewCraneRepository.Insert(crane);
        }
        public void UpdateCrane(ViewVesselCrane crane)
        {
            viewCraneRepository.Update(crane);
        }
        public void DeleteCrane(int id)
        {
            ViewVesselCrane crane = GetCrane(id);
            viewCraneRepository.Remove(crane);
            viewCraneRepository.SaveChanges();
        }
    }
}

服务/ICraneViewService.cs

using MyCompany.Data.Views;
using System;
using System.Collections.Generic;
using System.Text;

namespace MyCompany.Services.Views
{
    public interface ICraneViewService
    {
        IEnumerable<ViewVesselCrane> GetCranes();
        ViewVesselCrane GetCrane(int id);
        void InsertCrane(ViewVesselCrane crane);
        void UpdateCrane(ViewVesselCrane crane);
        void DeleteCrane(int id);
    }
}

使用 TagHelper 的网格代码是:

网格

<kendo-grid name="cranesGrid" height="250">
        <datasource type="DataSourceTagHelperType.Ajax" page-size="80">
            <transport>                    
                <read url="@Url.Action("CraneRead", "Vessel")" />
            </transport>

        </datasource>

        <columns>
            <column field="DeckEquipmentId" title="Deck Equip. Id" />
            <column field="VesselId" title="VesselId" />
            <column field="TypeName" title="TypeName" />
            <column field="NumberOfCranes" title="No. Cranes" />
            <column field="CraneId" title="CraneId" />                

        </columns>
    </kendo-grid>

Controller /VesselController/GetCranes

请注意,我硬编码了一个 Id 只是为了测试目的,在生产中,Id 作为可选参数通过绑定(bind)到传输中网格的 data 选项的 javascript 函数传递给 Controller ​​。

 public ActionResult CraneRead([DataSourceRequest] DataSourceRequest request, int? id)
    {
        var data = _craneService.GetCranes().Where(x => x.VesselId == 21059);
        var result = data.ToDataSourceResult(request);
        return Json(result);
    }

好的,这就是基本设置。现在对于 View ,它非常基本,将三个表连接在一起。如果我运行 View 并使用 SSMS 传入 VesselId,我会看到预期的结果。如果我运行该应用程序,我会看到正确数量的结果,但数据看起来是一样的,但事实并非如此。

查看VesselCrane

SELECT O.DeckEquipmentId, P.TypeName, I.SWL, I.NumberOfCranes, I.CraneTypeId, O.VesselId
FROM dbo.DeckEquipment AS O LEFT JOIN
                         dbo.Crane AS I ON O.DeckEquipmentId = I.DeckEquipmentId LEFT JOIN
                         dbo.CraneType AS P ON P.CraneTypeId = I.CraneTypeId

我不确定这个问题的根源是什么,考虑到 SQL 查询在我测试时有效,我只能假设我在我的应用程序中做错了什么,我需要一点帮助才能找到它。

最佳答案

我对此做了更多的研究,我发现问题出在 View 本身,EF 试图理解我在实体中使用“[Key]”注释定义的唯一键,但它不是它实际上是唯一的,因此重复了。我添加了一个额外的列(我不使用),它是唯一的,并对其应用了键注释,现在它可以工作了。

关于c# - 为什么我的 KendoUI 网格在我的 ASP.NET Core 应用程序中显示重复记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57760089/

相关文章:

c# - 使用具有流畅验证的自定义验证响应

c# - Configuration.GetValue<T> 返回 null 但 Bind 有效

c# - 将C#数组传递给C++ vector

c# - 在不使用LINQ的通用Windows库中使用SQLite

mysql - 如何生成一个查询,该查询考虑来自一个表的数据并相应地为mysql中的不匹配字段填充0

linux - 通过 Azure Devops 将 .Net Core 3.1 Web 应用部署到 Azure Linux Web 服务时出错

c# - 如何在单个 mvc3 View 中添加两个表?

c# - 支持翻译为sql的内联方法

mysql - 使用 MySQL 分区来加速并发删除和选择?

sql - 由于磁盘空间/内存错误而更改表中的数据类型