0

I have the following database:

Database model

An artist can have many titles. A title can be either a song or an album. In my code I have the following configuration:

 public class AlbumConfiguration : EntityTypeConfiguration<AlbumDetail>
{
    public AlbumConfiguration()
    {
        this.HasKey(a => a.Id);
        this.Property(a => a.Title);

        this.HasRequired(a => a.TitleDetail).WithOptional(t => t.Album);    
        this.ToTable("Albums");
    }
}

public class ArtistConfiguration : EntityTypeConfiguration<ArtistDetail>
{
    public ArtistConfiguration()
    {
        this.HasKey(a => a.Id);
        this.Property(a => a.Name);

        this.ToTable("Artists");

    }
    public class TitleConfiguration : EntityTypeConfiguration<TitleDetail>
{
    public TitleConfiguration()
    {
        this.HasKey(a => a.Id);
        this.Property(a => a.Title);

        this.HasRequired(a => a.Artist).WithMany(j => j.Titles).HasForeignKey(a => a.ArtistId);

        this.ToTable("Titles");
    }
}

This is what I do when I want a query to display the albums for one artist:

 IQueryable<AlbumDetail> artistDetail =
            base.Query.Where(x => x.Id == artistId).Include("Titles").OfType<AlbumDetail>();
        return artistDetail.ToList();

This is the query that was generated:

SELECT 
[Extent1].[Id] AS [Id], 
'0X0X' AS [C1], 
[Extent2].[Title] AS [Title], 
[Extent2].[ArtistId] AS [ArtistId], 
[Extent3].[Id] AS [Id1], 
[Extent1].[TitleDetail_Id] AS [TitleDetail_Id], 
[Extent4].[Id] AS [Id2]
FROM    [dbo].[Albums] AS [Extent1]
INNER JOIN [dbo].[Titles] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
LEFT OUTER JOIN [dbo].[Songs] AS [Extent3] ON [Extent1].[Id] = [Extent3].[TitleDetail_Id]
LEFT OUTER JOIN [dbo].[Albums] AS [Extent4] ON [Extent1].[Id] = [Extent4].[TitleDetail_Id]

When I now want a query to display the albums owned by an artist I get the following error :

Invalid column name 'TitleDetail_Id'. Invalid column name 'AlbumDetail_Id'. Invalid column name 'TitleDetail_Id'. Invalid column name 'TitleDetail_Id'. Invalid column name 'TitleDetail_Id'. Invalid column name 'TitleDetail_Id'. Invalid column name 'Song_Id'. Invalid column name 'AlbumDetail_Id1'. Invalid column name 'TitleDetail_Id'. Invalid column name 'Song_Id'. Invalid column name 'AlbumDetail_Id1'.

Can anyone help me?

Bart Schelkens
  • 1,235
  • 4
  • 21
  • 45
  • see this article : http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt – Iraj Jan 21 '15 at 08:16
  • and useful link : http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application – Iraj Jan 21 '15 at 08:18
  • I found those links as well, but I don't find the solution for my problem. – Bart Schelkens Jan 21 '15 at 09:06

0 Answers0