【问题标题】:API returning many rows that are exactly the sameAPI 返回许多完全相同的行
【发布时间】:2013-06-22 08:35:57
【问题描述】:

我有两个应用程序:

  1. API

  2. Web 表单应用程序

我在 API 中使用 Entity Framework 5。

Web 表单应用程序进行 API 调用并检索一些数据。

无论出于何种原因,当我取回数据时,它给了我几行完全相同的数据。

这里的问题是,当我使用 SQL Server Profiler 并查看查询时,查询是正确的,如果我采用此查询并在 SQL 中运行它,结果是正确的。但是,在 Web Forms App 中,很多数据都以相同的方式返回。

看看这两张截图——

运行应用程序:

获取实体框架生成的SQL查询并运行到SQL中:

如你所见,这让我很困惑......

有人知道这里有什么问题吗?

这是我的 EF 模型:

实体框架生成的查询:

{SELECT 
[Extent1].[dataSource] AS [dataSource], 
[Extent1].[ShowId] AS [ShowId], 
[Extent1].[Title] AS [Title], 
[Extent1].[EpisodeId] AS [EpisodeId], 
[Extent1].[EpisodeTitle] AS [EpisodeTitle], 
[Extent1].[Genre] AS [Genre], 
[Extent1].[ShowTypeDescription] AS [ShowTypeDescription], 
[Extent1].[DirectorName] AS [DirectorName], 
[Extent1].[ReleaseYear] AS [ReleaseYear], 
[Extent1].[SeasonEpisode] AS [SeasonEpisode]
FROM (SELECT 
      [TVData_VW_ShowList].[dataSource] AS [dataSource], 
      [TVData_VW_ShowList].[ShowId] AS [ShowId], 
      [TVData_VW_ShowList].[Title] AS [Title], 
      [TVData_VW_ShowList].[EpisodeId] AS [EpisodeId], 
      [TVData_VW_ShowList].[EpisodeTitle] AS [EpisodeTitle], 
      [TVData_VW_ShowList].[Genre] AS [Genre], 
      [TVData_VW_ShowList].[ShowTypeDescription] AS [ShowTypeDescription], 
      [TVData_VW_ShowList].[DirectorName] AS [DirectorName], 
      [TVData_VW_ShowList].[ReleaseYear] AS [ReleaseYear], 
      [TVData_VW_ShowList].[SeasonEpisode] AS [SeasonEpisode]
      FROM [dbo].[TVData_VW_ShowList] AS [TVData_VW_ShowList]) AS [Extent1]
WHERE [Extent1].[Title] LIKE @p__linq__0 ESCAPE '~'}

我通过进入调试模式得到了这个查询。如果我在实际数据库中运行它,它会返回正确的结果。

控制器代码:

公共类 ShowsController : ApiController { 私有只读 TVDataEntities 数据库;

public ShowsController()
{
    db = new TVDataEntities();
    db.Configuration.ProxyCreationEnabled = false;
}

public IEnumerable<TVData_VW_ShowList> GetTVData_VW_ShowList(string dataSource = null, string title = null,
                                                             string episodeTitle = null, string genre = null,
                                                             string showTypeDescription = null,
                                                             string directorName = null,
                                                             string releaseYear = null,
                                                             string seasonEpisode = null)
{
    var query = from s in db.TVData_VW_ShowList select s;

    if (dataSource != null)
    {
        if (dataSource != "all")
        {
            query = query.Where(s => s.dataSource.Contains(dataSource));
        }
    }

    if (title != null)
    {
        query = query.Where(s => s.Title.Contains(title));
    }

    if (episodeTitle != null)
    {
        query = query.Where(s => s.EpisodeTitle.Contains(episodeTitle));
    }

    if (genre != null)
    {
        query = query.Where(s => s.Genre.Contains(genre));
    }

    if (showTypeDescription != null)
    {
        query = query.Where(s => s.ShowTypeDescription.Contains(showTypeDescription));
    }

    if (directorName != null)
    {
        query = query.Where(s => s.DirectorName.Contains(directorName));
    }

    if (releaseYear != null)
    {
        query = query.Where(s => s.ReleaseYear.ToString().Contains(releaseYear));
    }

    if (seasonEpisode != null)
    {
        query = query.Where(s => s.SeasonEpisode.Contains(seasonEpisode));
    }

    return query.ToList();
}

}

【问题讨论】:

  • 我会一步一步调试这个。先从源头做起;忽略您的 Web 窗体应用程序并自行调用 API。你得到重复的数据吗?您的 Entity Framework 集合是否包含正确数量的结果?
  • 如果我通过浏览器进行 API 调用,它也会返回错误的数据。这对我来说没有任何意义,因为生成的查询是正确的,并且在 SQL 中运行时,返回正确的行数...
  • 能否在代码中设置断点(或写入磁盘或任何其他调试方法)以验证实体框架的结果?基本上只需验证 EF 返回的内容与您在链中稍后可能对结果集执行的操作相比。
  • 另外,如果您认为确实是 EF 向您返回了重复记录,您应该将代码发布到您要获取数据的位置。
  • 我在我的 OP 中添加了从 EF 获得的查询

标签: c# asp.net entity-framework-5 asp.net-web-api


【解决方案1】:

问题已解决。这是 Linq 查询。

这是正确的:

IQueryable<ETSShows> query = from shows in db.ETS_Shows
                                         from episodes in
                                             db.ETS_Episodes.Where(v => v.ShowId == shows.ShowId).DefaultIfEmpty()
                                         from genres in
                                             db.ETS_LKP_Genres.Where(s => s.GenreCode == shows.GenreCode).DefaultIfEmpty()
                                         from showTypes in
                                             db.ETS_LKP_ShowTypes.Where(z => z.ShowTypeCode == shows.ShowTypeCode).DefaultIfEmpty()
                                         from directors in
                                             db.ETS_Directors.Where(p => p.ShowId == shows.ShowId).DefaultIfEmpty()
                                         select new ETSShows
                                             {
                                                 DataSource = "ETS",
                                                 Title = shows.Title,
                                                 EpisodeTitle = episodes.EpisodeTitle,
                                                 Genre = genres.GenreDescription,
                                                 ShowTypeDescription = showTypes.ShowTypeDescription,
                                                 DirectorName = directors.Name,
                                                 ReleaseYear = (int)shows.ReleaseYear,
                                                 SeasonEpisode = episodes.SeasonEpisode,
                                                 ShowId = shows.ShowId,
                                                 EpisodeId = episodes.EpisodeId
                                             };

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 2014-07-18
    • 2019-07-29
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 2018-03-02
    相关资源
    最近更新 更多