Why EF Core is generating the wrong name of the navigation field when creating SELECT clause causing exception?

1

I have a asp.net core 2.2. project with EF Core. I'm facing this issue. Look at the table below that has two foreign keys:

enter image description here

When I run scaffolding, the above table called RubrosContablesCtasBanc, EF created the class and Fluent DbContext as it follows:

public partial class RubrosContablesCtasBanc
{
    public int Id { get; set; }
    public int IdCuentaBancaria { get; set; }
    public int IdRubroContable { get; set; }

    public virtual CuentasBancarias IdCuentaBancariaNavigation { get; set; }
    public virtual RubrosContables IdRubroContableNavigation { get; set; }
}

in the OnModelCreating of DbContext:

 modelBuilder.Entity<RubrosContablesCtasBanc>(entity =>
        {
            entity.HasOne(d => d.IdCuentaBancariaNavigation)
                .WithMany(p => p.RubrosContablesCtasBanc)
                .HasForeignKey(d => d.IdCuentaBancaria)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_RContablesCtaBanc_CuentasBancarias");

            entity.HasOne(d => d.IdRubroContableNavigation)
                .WithMany(p => p.RubrosContablesCtasBanc)
                .HasForeignKey(d => d.IdRubroContable)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_RContablesCtaBanc_RubrosContables");
        });

However, when I run a query it generates an error due to the fields that are part of the relationship:

SELECT [r].[Id], [r].[IdCuentaBancaria], [r].[IdCuentaBancariaNavigationId], [r].[IdRubroContable], [r].[IdRubroContableNavigationId]
FROM [RubrosContablesCtasBanc] AS [r]"
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'IdCuentaBancariaNavigationId'.
Invalid column name 'IdRubroContableNavigationId'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
ClientConnectionId:808591b0-9ee9-4f35-92d0-e962d48184fa

I don't understand how EF is creating the query to put wrong field names adding the Id at the end of the navigation fields.

Updated: I want to make clear that this is not happening in other situations, for example, I have another class entity created by scaffolding:

enter image description here

however, queries are working fine:

enter image description here

As you can see above, the navigation property IdPersonaNavigation is loading its data. Like this, many others work fine.

c#
.net
entity-framework
asp.net-core
entity-framework-core
asked on Stack Overflow May 19, 2020 by Aldemar Cuartas Carvajal • edited May 22, 2020 by Aldemar Cuartas Carvajal

1 Answer

1

The properties with NavigationId suffix, I think, come from EF core conventions.

Maybe there is bug in EF core or EF core generator somewhere caused by Id in prefix. As workaround try ForeignKeyAttribute, for example:

public partial class RubrosContablesCtasBanc
{
    public int Id { get; set; }
    public int IdCuentaBancaria { get; set; }
    public int IdRubroContable { get; set; }

    [ForeignKey("IdCuentaBancaria")]
    public virtual CuentasBancarias IdCuentaBancariaNavigation { get; set; }
    [ForeignKey("IdRubroContable")]
    public virtual RubrosContables IdRubroContableNavigation { get; set; }
}
answered on Stack Overflow May 19, 2020 by Guru Stron

User contributions licensed under CC BY-SA 3.0