0

I have a model:

public class ConstRouteFamily
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public long Id { get; set; }

    public long RouteId { get; set; }
    public long FamilyId { get; set; }

    public virtual ICollection<Family> Families { get; set; }
} 

and the table:

CREATE TABLE [dbo].[ConstRouteFamilies]
(
    [Id] [BIGINT] IDENTITY(1,1) NOT NULL,
    [RouteId] [BIGINT] NOT NULL,
    [FamilyId] [BIGINT] NULL,

    CONSTRAINT [PK_ConstRouteFamilies] 
        PRIMARY KEY CLUSTERED ([Id] ASC)
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[ConstRouteFamilies] WITH CHECK 
    ADD CONSTRAINT [FK_ConstRouteFamilies_ConstRoutes] 
        FOREIGN KEY([RouteId]) REFERENCES [dbo].[ConstRoutes] ([Id])
GO
ALTER TABLE [dbo].[ConstRouteFamilies] CHECK CONSTRAINT [FK_ConstRouteFamilies_ConstRoutes]
GO

ALTER TABLE [dbo].[ConstRouteFamilies] WITH CHECK 
    ADD CONSTRAINT [FK_ConstRouteFamilies_Families] 
        FOREIGN KEY([FamilyId]) REFERENCES [dbo].[Families] ([Id])
GO

ALTER TABLE [dbo].[ConstRouteFamilies] CHECK CONSTRAINT [FK_ConstRouteFamilies_Families]
GO

Now, when I try to run the query:

var families = db.ConstRouteFamilies
                 .Where(crf => crf.RouteId.Equals(route.Id))
                 .ToList();

I get an exception

Invalid column name 'ConstRoute_Id'

I looked at the SQL that was generated:

SELECT 
    1 AS [C1], 
    [Extent1].[Id] AS [Id], 
    [Extent1].[RouteId] AS [RouteId], 
    [Extent1].[FamilyId] AS [FamilyId], 
    [Extent1].[ConstRoute_Id] AS [ConstRoute_Id]
FROM 
    [dbo].[ConstRouteFamilies] AS [Extent1]

Where did that ConstRoute_Id column came from? And how do I remove/fix that error?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Erez Konforti
  • 243
  • 5
  • 19
  • 1
    Possible duplicate of [Entity Framework - Invalid Column Name '\*\_ID"](https://stackoverflow.com/questions/19959256/entity-framework-invalid-column-name-id) – Anton Sizikov May 08 '18 at 09:14

1 Answers1

0

I think it is a foreign key issue with the Families table. You can specify the foreign key like this -

     [ForeignKey("FamilyId")]
public virtual ICollection<Family> Families { get; set; }

See if that helps.

Tajuddin
  • 73
  • 3
  • 13