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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.