EF Core searching for non-existent table when accessing linking-table entries

29 views Asked by At

I use EF Core (nuget package versions: 8.0.2) and the DB-first aproach.

I have created the DB models from my DB (using the automatic scaffolding), with attributes included, but when I try to fetch a document from the DB, I get this error:

Microsoft.Data.SqlClient.SqlException: 'Invalid object name 'Document.CitizenRequestKindDocument'.'

This table does not exist and I am not sure why it doesn't use the names from the attributes to fetch data.

This happens when trying to access the property IdCitizenRequestKinds from below:

[Table("Document", Schema = "Document")]
public partial class Document
{
    [Key]
    public int IdDocument { get; set; }
    ...
    ...
    [ForeignKey("IdDocument")]
    [InverseProperty("IdDocuments")]
    public virtual ICollection<CitizenRequestKind> IdCitizenRequestKinds { get; set; } = new List<CitizenRequestKind>();
    ...
    ...
}



[Table("CitizenRequestKind", Schema = "Document")]
public partial class CitizenRequestKind
{
    [Key]
    public int IdCitizenRequestKind { get; set; }
    ...
    ...
    [ForeignKey("IdCitizenRequestKind")]
    [InverseProperty("IdCitizenRequestKinds")]
    public virtual ICollection<Document> IdDocuments { get; set; } = new List<Document>();
}

The special thing about this DB-model is that it uses a linking table in the DB:

DB schema

Note that the linking table is not even present on the auto-generated DB-models.

I have tried defining a relationship like that, but it didn't seem to change anything:

        var docBuilder = modelBuilder.Entity<Document>();
        docBuilder.HasMany(e => e.IdCitizenRequestKinds).WithMany(e => e.IdDocuments);

How can this work, preferably without introducing the linking table into the DB models?

0

There are 0 answers