I'm trying to retrieve the VideoCollection items from the database, but getting {"Invalid column name 'User_Id'."}
- when I set a breakpoint I can see that the EF is trying to select User_Id column but it doesn't exist in the Cs object or the database schema. (It does exist in another table but that shouldn't matter). Is there anyway to debug why this is happening?
Asked
Active
Viewed 5,592 times
9

greenimpala
- 3,777
- 3
- 31
- 39
2 Answers
12
Do you have a User class that has a reference to a VideoCollection? If so, you probably need to explicitly define the relationship between the two. As it is, it looks like EF is inferring that the VideoCollection should contain a foreign key, User_Id
that defines the relationship.

tvanfosson
- 524,688
- 99
- 697
- 795
-
You're almost there - I have an intermediary table PurchasedCollections - that has UserId and VideoCollectionId (both as foreign keys). This should define the relationship explicitly? I removed this intermediary from the Cs objects and in the DbContext (as I'd never need to access it directly). Could thishave caused the problem? – greenimpala Sep 25 '11 at 14:44
-
2@st3 you need to either rename the table to User_VideoCollections or override `OnModelCreating` and explicitly establish the connection through the `PurchasedCollections` table. EF (code first) conventions expect that either you have a direct FK relationship or a join table using the names of the entities. If you deviate from that, you need to tell it how to make the connection. Something like `builder.Entity
().HasMany( u => u.Collections ).WithRequired().ToTable( "PurchasedCollections" );` – tvanfosson Sep 25 '11 at 14:50 -
this seems to have worked, thanks. Are you saying that any many to many relationship needs to be declared with this naming convention (unless you explicitly define the relationship?). – greenimpala Sep 25 '11 at 15:21
-
@st3 - no, only if you deviate from the convention. – tvanfosson Sep 25 '11 at 15:27
-
I found it useful to go with the `InverseProperty` to solve the same issue: http://stackoverflow.com/questions/43841065/entityframework-is-trying-to-fetch-non-existing-column/43841388 – Mando May 08 '17 at 16:48