Search code examples
c#asp.netasp.net-web-apientity-framework-core

Data annotations not recognized by Entity Framework Core in one ASP.NET project but work properly in another


I am using Entity Framework Core 3.1 with a "code-first" approach for 2 ASP.NET projects on .NET 4.8. "Code first" is in quotes because this is really a migration from EF 6 where we've deleted the .edmx file and revised the DbContext. The database schema is locked and we are only looking to get this functioning, not allow it to adapt to new db changes (and re-generating the DbContext/entities is not an option because it breaks an unbelievable amount of code).

Anyways - I have one web project working great (as well as several console projects and windows services), but the last ASP.NET Web API project keeps throwing errors, at first when initializing the DbContext and then when running queries.

All of the exceptions are due to the fact that the entity class data annotations are not being recognized.

Here's an example of an entity which includes all the attributes that are in use:

namespace Model.EF
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;    
    
    [Table("FaxStatus", Schema = "Global")]
    public partial class FaxStatus
    {
        [Key]
        public int FaxId { get; set; }
        public string XMLStatus { get; set; }
        public string XMLStatusMessage { get; set; }
        public string XMLPath { get; set; }
    }
}

The attributes define the table, schema, and key for this entity.

Again, this works fine in half a dozen other projects. In the problem project, at DbContext initialization I got an error that there was no key defined and had to add an OnModelCreating HasKey entry to make it happy (this is only for keys that don't follow convention of course).

Then, when I had added all those entries, making a query like this:

using (var db = new EFContext())
{
    var statuses = db.FaxStatuses.FirstOrDefault();
}

This code throws the error:

Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'FaxStatuses'

I even checked the SQL Server Profiler -- it's trying to use the DbSet<FaxStatus> property name (FaxStatuses) as the table name instead of one specified in the attribute (or even the class name).

In other words, the SQL should be:

SELECT TOP 1 * FROM Global.FaxStatus

But instead, it is:

SELECT TOP 1 * FROM Global.FaxStatuses

I've verified that all the EF Core libraries (base, SqlServer, Relational, Proxies, Abstractions) are installed in the project with the same versions as the working project. I've looked in all the application startup code and can't find anything related to EF Core configuration that might mess things up.

And the DbContext and entity classes are in a shared library used by all of these different projects.

Why is this one Web API project ignoring the attributes on these classes?

Edit: A couple people have asked about FaxStatuses; it is declared in my DBContext class like so:

public DbSet<FaxStatus> FaxStatuses { get; set; }

I also have a reference to it in OnModelCreating:

modelBuilder.Entity<FaxStatus>();

Solution

  • The resolution ended up being downgrading System.ComponentModel.Annotations from 5.0.0 to 4.7.0 in the problematic WebAPI project. All the attributes were recognized immediately.