我已经使用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 query returns the same names even though they should be different
以下是我配置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; }
}
答案 0 :(得分:0)
好的,我修好了。我弄乱了关系映射。
曲目和播放列表应如下所示:
public class Track
{
...
[ForeignKey("TrackId")]
public virtual ICollection<PlaylistTrack> PlaylistTracks { get; set; }
}
public class Playlist
{
...
[ForeignKey("PlaylistId")]
public virtual ICollection<PlaylistTrack> PlaylistTracks { get; set; }
}
干杯