Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core 7 Reverse Engineering - Navigation properties throwing "invalid column *" exception #1600

Closed
MKSoftwaretech opened this issue Nov 29, 2022 · 3 comments
Labels
question Further information is requested

Comments

@MKSoftwaretech
Copy link

MKSoftwaretech commented Nov 29, 2022

We are using the database-first approach and recently switched from EF Core 6 to EF Core 7. So I used the reverse engineering tool with EF Core 7, which now seem to have a problem with some of our navigation properties.

Given the following simplified schema (german):

Table Mitarbeiter (employee)
Column - Mitarbeiter_Nr (int - no PK)

Table Rechnung_Kopf (invoice)
Column - Sachbearbeiternummer (int, FK to Mitarbeiter.Mitarbeiter_Nr)
Column - Vertreternummer (int, FK to Mitarbeiter.Mitarbeiter_Nr)

The RE-Tool provides the following result (simplified):

[Table("Mitarbeiter", Schema = "dbo")]
[Index("MitarbeiterNr", Name = "AK_Mitarbeiter", IsUnique = true)]
public partial class Mitarbeiter
{
    [Required]
    [Column("Mitarbeiter_Nr")]
    public int? MitarbeiterNr { get; set; }

    public virtual ICollection<RechnungKopf> RechnungKopfSachbearbeiternummerNavigation { get; } = new List<RechnungKopf>();

    public virtual ICollection<RechnungKopf> RechnungKopfVertreternummerNavigation { get; } = new List<RechnungKopf>();
}

[Table("Rechnung_Kopf", Schema = "dbo")]
public partial class RechnungKopf
{
    public int? Sachbearbeiternummer { get; set; }

    public int? Vertreternummer { get; set; }

    public virtual Mitarbeiter SachbearbeiternummerNavigation { get; set; }

    public virtual Mitarbeiter VertreternummerNavigation { get; set; }
}

Now whenever we query some data of RechnungKopf, the following exception will occur:

Invalid column name "SachbearbeiternummerNavigationMitarbeiterNr".
Invalid column name "VertreternummerNavigationMitarbeiterNr".

Looking at the other (working) navigation properties there is always the [InverseProperty] and [ForeignKey] attribute, which is missing here. Adding it by hand won't fix the error though.

When we generated the code with EF Core 6, it was exactly the same, but we didn't get the errors mentioned above.
What is going wrong here and how to fix it?

Provide technical details

  • EF Core version in use: EF Core 7

  • Is Handlebars used: no

  • Is T4 used: no

  • Is .dacpac used: no

  • EF Core Power Tools version: 2.5.1206.0

  • Database engine: SQL Server

  • Visual Studio version: Visual Studio 2022

Error Stacktrace:

bei Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
bei Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
bei Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
bei Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
bei Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
bei Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
bei Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
bei Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
bei Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
bei Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
bei Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
bei Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
bei Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
bei Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.InitializeReader(Enumerator enumerator)
bei Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.<>c.b__21_0(DbContext _, Enumerator enumerator)
bei Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
bei Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.Enumerator.MoveNext()
bei System.Collections.Generic.List1..ctor(IEnumerable1 collection)
bei System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
bei FlexoSoftPlus.Database.PpgDbContext.HoleRechnungsdetails(String rechnungsnummer) in C:\Users\extern7\source\repos\FlexoSoftPlusV1\FlexoSoftPlus.Database\PartialExtents\DbContext\Rechnungswesen.cs: Zeile1606
bei FlexoSoftPlus.Modules.Rechnungsverwaltung.ViewModels.RechnungsverwaltungViewModel.d__103.MoveNext() in C:\Users\extern7\source\repos\FlexoSoftPlusV1\FlexoSoftPlus.Modules\Rechnungsverwaltung\ViewModels\RechnungsverwaltungViewModel.Logik.cs: Zeile943

@ErikEJ
Copy link
Owner

ErikEJ commented Nov 29, 2022

Duplicate of #1554 ?? - basically you will have to wait for a fix, maybe until primo Feb 2023

@btcalit
Copy link

btcalit commented Nov 30, 2022

@ErikEJ, I'm a bit confused by the unique constraint part of the title in #29418 I'm looking at my project and when generating using EF Core 7 and Use DataAnnotation attributes to configure the model, literally every single .HasForeignKey() is gone from the context. The [ForeignKey("ColumnId")] attributes are still there on the entities themselves, as they were under EF Core 6. However under EF Core 6 both .HasForeignKey() and [ForeignKey("ColumnId")] were present. These are you basic foreign key references from a dependent tables to the primary key on the principal tables.

I'm trying to understand if there are any consequences here? Or is it OK as long as either .HasForeignKey() or [ForeignKey("ColumnId")] is present.

@ErikEJ
Copy link
Owner

ErikEJ commented Nov 30, 2022

@btcalit please ask in the EF Core repo

@ErikEJ ErikEJ added the question Further information is requested label Dec 2, 2022
@ErikEJ ErikEJ closed this as not planned Won't fix, can't repro, duplicate, stale Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants