Home > Mobile >  EFCore looks for xxxId as ForeignKey instead of xxx
EFCore looks for xxxId as ForeignKey instead of xxx

Time:08-12

I use EFCore.

In my Entity, there are this Properties:

    [Column("ReleasedFrom")]
    public string ReleasedFromPersNr { get; set; }
    public LoginUser ReleasedFrom { get; set; }

ReleasedFromPersNr is the ForeignKey-Property for the NavigationProperty ReleasedFrom. The column on the Database is ReleaseFrom (like defined in the Column-Attribute). But EFCore uses the Column ReleaseFromId which generates this Exception:

Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'ReleasedFromId'.'

This is the generated SQL-Select:

SELECT [w].[ID], [w].[ReleasedFromId] FROM [WochenrapportStunden] AS [w]
  WHERE [w].[ID] = 255547

but it should be

SELECT [w].[ID], [w].[ReleasedFrom] FROM [WochenrapportStunden] AS [w]
  WHERE [w].[ID] = 255547

What need I to do, that EFCore uses ReleasedFrom instead ReleasedFromId?

CodePudding user response:

You need to use the [ForeignKey] attribute, rather than the [Column] attribute.

  • Related