.net mvc返回重复记录

时间:2015-01-21 20:51:45

标签: sql-server ajax asp.net-mvc json

我有一个ajax / json调用正在返回 正确数字 的记录,但它们都是 相同的记录 ,第一条记录。

SQL视图:

ALTER view [dbo].[v_EMS_BM_Organization_Detail] as
select a.OrgID
,   a.Organization
,   a.inactive
,   b.buID
,   b.BusinessUnit
,   count(distinct c.Budget) Budgets
,   count(distinct d.BEMS) People
from t_EMS_BM_Organization a
left join t_EMS_BM_Business_Unit b
on a.OrgID = b.OrgID
left join t_EMS_BM_Budget c
on b.buID = c.buID
left join t_EMS_BM_Person d
on c.Budget = d.Budget
group by a.OrgID
,   a.Organization
,   a.inactive
,   b.buID
,   b.BusinessUnit
GO 

在SQL Server中查询时:

select *
from v_EMS_BM_Organization_Detail
where OrgID= 6

返回:

OrgID   Organization    inactive    buID    BusinessUnit    Budgets People
6       E3001       0       27      Manufacturing Services Staff    1   5
6       E3001       0       43      BASN    1   0
6       E3001       0       45      Special Assignment  1   0

我的模特:

namespace EMSBM.Models
{
    using System;
    using System.Collections.Generic;

    public partial class v_EMS_BM_Organization_Detail
    {
        public int OrgID { get; set; }
        public string Organization { get; set; }
        public Nullable<bool> inactive { get; set; }
        public Nullable<int> buID { get; set; }
        public string BusinessUnit { get; set; }
        public Nullable<int> Budgets { get; set; }
        public Nullable<int> People { get; set; }
    }
}

我的控制器:

[HttpPost]
public JsonResult OrgDetails(int ID)
{
    List<v_EMS_BM_Organization_Detail> orgDetail = new List<v_EMS_BM_Organization_Detail>();

    orgDetail = db.v_EMS_BM_Organization_Detail.Where(x => x.OrgID == ID).OrderBy(x => x.BusinessUnit).ToList();

    return this.Json(new
    {
        Result = (from obj in orgDetail
                  select new
                  {
                      OrgID = obj.OrgID
                      ,
                      Organization = obj.Organization
                      ,
                      inactive = obj.inactive
                      ,
                      buID = obj.buID
                      ,
                      BusinessUnit = obj.BusinessUnit
                      ,
                      Budgets = obj.Budgets
                      ,
                      People = obj.People
                  })
    });
}

我的Ajax / Json电话:

$('#organization-table-body').delegate('tr', 'click', function () {
    var OrgID = $(this).attr('id');
    OrgID = OrgID.substr(2, OrgID.length);

    $.ajax({
        type: 'post',
        url: '/OrganizationDetail/OrgDetails',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ ID: OrgID }),
        success: function (data) {
            $('#org-detail-body').empty();
            var html = '';
            $.each(data['Result'], function (i, item) {
                if (i == 0) {
                    $('#org-name').html(item.Organization +' Details');
                    $('#OrgID').val(OrgID);

                    $('#btn-active-status').removeClass('btn-primary');
                    $('#btn-active-status').removeClass('btn-danger');
                    if (item.inactive == true) {
                        $('#btn-active-status').html('Activate Organization');
                        $('#btn-active-status').addClass('btn-primary');
                    } else {
                        $('#btn-active-status').html('Deactivate Organization');
                        $('#btn-active-status').addClass('btn-danger');
                    }
                }
                html += '<tr id="tr' + item.buID + '" class="cursor-pointer"><td>' + item.BusinessUnit + '</td>';
                html += '<td>' + item.Budgets + '</td><td>' + item.People + '</td></tr>';
            });
            $('#org-detail-body').html(html);
            $('#org-detail-div').modal('show');
        },
        error: function (ex) {
            alert(ex.responseText);
        }
    });

当在上面的sql server查询中传递6时,我得到三条记录,这是正确的。但这三个记录只是第一个记录:

Business Unit   Budgets     People
BASN            1           0
BASN            1           0
BASN            1           0

在VS 2012中进行调试时,无论我传递的是什么ID,我都只能将第一条记录重复正确的X次。

1 个答案:

答案 0 :(得分:0)

此问题与您的视图中缺少主键有关。 EF试图用来识别唯一失败的行。

看起来你正在使用Database First,那个模型类是生成代码的?如果是这样,请在模型浏览器中查看它。 EF将猜测(通常非常糟糕)它应该用于构建主键的列。它包含密钥中的列,如果它知道它们不能是NULL并且如果不确定则排除列。

如果您愿意,可以使用ROW_NUMBER()为表格创建代理键,或者您可以使用现有列。无论哪种方式,要向EF提供其在键中包含哪些列的提示,您应该将列包裹在ISNULL()(强制包含)或NULLIF()(强制排除)中。

这里有一个关于这一切的教程:http://girlfromoutofthisworld.com/entity-framework-and-setting-primary-keys-on-views/