2
我有一種情況,EF試圖從表「VideoClip」中獲取字段值,我沒有要求它獲取。EF中的列名無效
完整生成的查詢看起來是這樣的:
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;
}
Howcome EF給我:Invalid column name 'Video_Id'
?
您可以請發佈您的LINQ查詢嗎? – IronMan84 2013-03-15 14:45:17
Video_Id將是它期待的視頻表的外鍵。檢查你的視頻模式類和流利的映射,看看它是否有多個/集合引用NewsItemVideoClip。 – AaronLS 2013-03-18 05:06:33
Omg!我爲此做了什麼? ))) – Roman 2013-03-18 05:12:47