Search code examples
c#sql-serverentity-frameworklinqef-core-3.0

Query data after scaffolding the existing database. Error: The navigation '' cannot be added because it targets the keyless entity type


My problem is as describe in the title. I am using EF core to scaffold an existing database. It auto generates all the table class files and DbContext class. Then I try to query the Offer table with Id = 4341. However I always get this error.

The navigation '' cannot be added because it targets the keyless entity type

I have seen someone asked this question before but no clear solution has been provided.

This is how I query the database:

using (var context = new Vmob_xbg178_CoreContext())
{
    var offer4341 = context.Offer.Where(s => s.Id == 4341);
}

[Error in DbContext class[1]


Solution

  • The problem is that MessageKeyTag table in your database dose not have a primary key. EF assumes every entity has key. Otherwise the table will be treated as a view.

    Please add a primary key to your keyless table then regenerate the code...