我有一种情况,EF 试图从表“VideoClip”中获取我没有要求它获取的字段值。
生成的完整查询如下所示:
SELECT
[Project1].[Id] AS [Id],
[Project1].[NewsItemId] AS [NewsItemId],
[Project1].[VideoClipId] AS [VideoClipId],
[Project1].[DisplayOrder] AS [DisplayOrder],
[Project1].[Video_Id] AS [Video_Id]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[NewsItemId] AS [NewsItemId],
[Extent1].[VideoClipId] AS [VideoClipId],
[Extent1].[DisplayOrder] AS [DisplayOrder],
[Extent1].[Video_Id] AS [Video_Id]
FROM [dbo].[NewsItemVideoClip] AS [Extent1]
WHERE [Extent1].[NewsItemId] = @p__linq__0
) AS [Project1]
ORDER BY [Project1].[DisplayOrder] DESC
但是,我的NewsItemVideoClip
实体类如下所示:
public class NewsItemVideoClip : Entity
{
public virtual int NewsItemId { get; set; }
public virtual int VideoClipId { get; set; }
public virtual int DisplayOrder { get; set; }
public virtual NewsItem NewsItem { get; set; }
public virtual VideoClip VideoClip { get; set; }
}
映射:
public class NewsItemVideoClipEntityMapping
: EntityTypeConfiguration<NewsItemVideoClip>
{
public NewsItemVideoClipEntityMapping()
{
//configure key and properties
HasKey(c => c.Id);
this.HasRequired(x => x.NewsItem)
.WithMany(x => x.NewsItemVideoClips)
.HasForeignKey(x => x.NewsItemId);
this.HasRequired(x => x.VideoClip)
.WithMany(x => x.NewsItemVideos)
.HasForeignKey(x => x.VideoClipId);
//configure table map
ToTable("NewsItemVideoClip");
}
}
LINQ 查询:
public IList<NewsItemVideoClip> GetVideoClips(int newsItemId)
{
var query = from nvc in _newsItemVideoClipRepository.GetAll()
where nvc.NewsItemId == newsItemId
orderby nvc.DisplayOrder descending
select nvc;
var newsItemVideoClips = query.ToList();
return newsItemVideoClips;
}
EF怎么给我:Invalid column name 'Video_Id'
?