Invalid column name in EF

code-first entity-framework


I have a situation where EF is trying to get a field value from the table "VideoClip" that I didn't ask it to get.

Database schema showing the tables NewsItemVideoClip, NewsItem, VideoClip and Video

The complete generated query looks like this:

[Project1].[Id] AS [Id], 
[Project1].[NewsItemId] AS [NewsItemId], 
[Project1].[VideoClipId] AS [VideoClipId], 
[Project1].[DisplayOrder] AS [DisplayOrder], 
[Project1].[Video_Id] AS [Video_Id]
    [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

However, my NewsItemVideoClip entity class looks like this:

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; }

The mapping:

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

The LINQ query:

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 gives me: Invalid column name 'Video_Id'?

1/15/2014 4:52:56 AM

Accepted Answer

Based on your diagram, VideoClip has a VideoId which I guess is your intended foreign key to Video. Video_Id would be the default foreignkey name for the foreign key in the abscence of mapping that would name it otherwise. So probably need a .HasForeignKey(x => x.VideoId); to your VideoClip mapping.

3/18/2013 5:10:30 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow