繁体   English   中英

渴望加载时EF6使用了错误的密钥

[英]EF6 is using the wrong key when eager loading

我已经使用SqlLite .NET提供程序在Chinook数据库上成功设置了EF6。

现在,“播放列表和曲目”是通过联结表PlaylistTracks映射的许多关系。

以下查询生成错误的SQL语句,其中Track映射到联结表上的PlaylistId(应为TrackId)。

var result = context.Playlists
                    .Where(p => p.Name == "Brazilian Music")
                    .SelectMany(pt => pt.PlaylistTracks)
                    .Include(pt => pt.Track).OrderBy(pt => pt.TrackId);

SQL跟踪:

SELECT 
[Extent1].[PlaylistId] AS [PlaylistId], 
[Extent2].[PlaylistId] AS [PlaylistId1], 
[Extent2].[TrackId] AS [TrackId], 
[Extent3].[TrackId] AS [TrackId1], 
[Extent3].[Name] AS [Name], 
[Extent3].[AlbumId] AS [AlbumId], 
[Extent3].[MediaTypeId] AS [MediaTypeId], 
[Extent3].[GenreId] AS [GenreId], 
[Extent3].[Composer] AS [Composer], 
[Extent3].[Milliseconds] AS [Milliseconds], 
[Extent3].[Bytes] AS [Bytes], 
[Extent3].[UnitPrice] AS [UnitPrice]
FROM   [Playlist] AS [Extent1]
INNER JOIN [PlaylistTrack] AS [Extent2] ON [Extent1].[PlaylistId] = [Extent2].[PlaylistId]
INNER JOIN [Track] AS [Extent3] ON [Extent2].[PlaylistId] = [Extent3].[TrackId]
WHERE 'Brazilian Music' = [Extent1].[Name]
ORDER BY [Extent2].[TrackId] ASC

在最后一次INNER JOIN结束的某个地方,我得到了

[Extent2]。[PlaylistId] = [Extent3]。[TrackId]

它应该是[Extent2]。[TrackId] = [Extent3]。[TrackId],其中[Extent2]是联结表。

如何使EF6映射到正确的外键?

感谢任何帮助。

看到我的原始问题, Linq查询返回相同的名称,即使它们应该不同

这是我配置POCO的方式

[DebuggerDisplay("{Name} (PlaylistId = {PlaylistId})")]
public class Playlist
{
    [Key]
    public int PlaylistId { get; set; }

    [Required, MaxLength(120)]
    public string Name { get; set; }

    [ForeignKey("TrackId")]
    public virtual ICollection<PlaylistTrack> PlaylistTracks { get; set; }
}

[DebuggerDisplay("{Name} (TrackId = {TrackId})")]
public class Track
{
    [Key]
    public int TrackId { get; set; }

    [Required, MaxLength(200)]
    public string Name { get; set; }

    public int? AlbumId { get; set; }

    [Required]
    public int MediaTypeId { get; set; }

    public int? GenreId { get; set; }

    [MaxLength(220)]
    public string Composer { get; set; }

    [Required]
    public int Milliseconds { get; set; }

    public int Bytes { get; set; }

    [Required]
    public decimal UnitPrice { get; set; }

    [ForeignKey("AlbumId")]
    public virtual Album Album { get; set; }

    [ForeignKey("GenreId")]
    public virtual Genre Genre { get; set; }

    [ForeignKey("MediaTypeId")]
    public virtual MediaType MediaType { get; set; }

    [ForeignKey("InvoiceLineId")]
    public virtual ICollection<InvoiceLine> InvoiceLines { get; set; }

    [ForeignKey("PlaylistId")]
    public virtual ICollection<PlaylistTrack> PlaylistTracks { get; set; }
}

[DebuggerDisplay("PlaylistId = {PlaylistId}, TrackId = {TrackId}")]
public class PlaylistTrack
{
    [Key, Column(Order = 1)]
    public int PlaylistId { get; set; }

    [Key, Column(Order = 2)]
    public int TrackId { get; set; }

    [ForeignKey("PlaylistId")]
    public virtual Playlist Playlist { get; set; }

    [ForeignKey("TrackId")]
    public virtual Track Track { get; set; }

}

好的,我已经解决了。 我搞砸了关系映射。

曲目和播放列表应如下所示:

public class Track
{
  ...
  [ForeignKey("TrackId")]
  public virtual ICollection<PlaylistTrack> PlaylistTracks { get; set; }
}

public class Playlist
{
  ...
  [ForeignKey("PlaylistId")]
  public virtual ICollection<PlaylistTrack> PlaylistTracks { get; set; }
}

干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM