Entity framework Web API方法返回的重复记录

Entity framework Web API方法返回的重复记录,entity-framework,asp.net-web-api,odata,Entity Framework,Asp.net Web Api,Odata,我创建了一个我认为是在数据库视图上选择的简单控制器。但是,第一条记录在JSON结果中多次返回。例如,当使用Odata进行筛选时,预期会有7条不同的记录,但是会返回7条相同的记录 以下是控制器代码: using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq

我创建了一个我认为是在数据库视图上选择的简单控制器。但是,第一条记录在JSON结果中多次返回。例如,当使用Odata进行筛选时,预期会有7条不同的记录,但是会返回7条相同的记录

以下是控制器代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using MyAPI.WebAPI.Models;
using System.Web.OData;
using System.Security.Principal;
using System.Web;

namespace MyAPI.WebAPI.Controllers
{
    public class MyViewController : ApiController
    {
        private MyEntities db = new MyEntities();

        // GET
        [EnableQuery] // Enables OData query integration
        public IHttpActionResult GetMetrics()
        {
            return Ok(db.ViewResults.AsQueryable());
        }
    }
}
在return语句上使用断点时,我可以看到查询中所有4000个结果的返回位置,OData似乎只过滤我需要的7条记录,但当它通过JSON字符串发送时,它是重复的


有人经历过类似的事情吗?

好的,原来我没有在Entity Framework中正确设置键。我最后使用的键是位置、日期和度量的复合键。在框架模型中选择这些键后,所有键都按预期工作