EF Weirdness - invalid column of XXXXX

 Some days there is just weirdness that makes no sense. 

My error message was "invalid column of StoreMenuId".  The error message did not make any sense.  I had no column called that in any table or view in my database.  

Also wrong was when I looked at the properties on the table after I eventually found them in a weird place under EntityType in Intellisense.  I had all the correct columns PLUS this mysterious column.  

EntityType: StoreMenuDevices
  Properties: 
    Id (int) Required PK AfterSave:Throw ValueGenerated.OnAdd IdentityColumn
    CreatedOn (DateTime) Required 
    CurrentMenu (int?)   
    DeviceName (string) MaxLength(50) Annotations: MaxLength: 50 
    LastUpdatedOn (DateTime) Required 
    SkusMissing (bool) Required 
    Status (string) MaxLength(50) Annotations: MaxLength: 50 
    StoreMenuId (no field, int?) Shadow FK Index 
    UpdateId (int) Required 
  Keys:     Id PK
  Foreign keys:     StoreMenuDevices {'StoreMenuId'} -> StoreMenu {'Id'} ClientSetNull ToDependent: StoreMenuDevices

Once I finally saw this, I knew the problem.  Turns out the real problem was I had not attributed a foreign key properly in the model.  The mysterious foreign key was named StoreMenuId.

Was:

public int UpdateId { get; set; }

Should be:

[ForeignKey("StoreMenu")]

[InverseProperty("Id")]
public int UpdateId { get; set; }
public StoreMenu? StoreMenu { get; set; }

The UpdateId is a foreign key that should match up against "StoreMenu.Id" column.  I guess the error message stripped the period for some reason.





Comments

Popular posts from this blog

Upgrading to .NET8 from desktop versions 4.8.X

GHL Chat Bots for Webpage

GHL > Set website so shorter URL address