question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reverse Engineer with option "UseDbContextSplitting": true gives compiler error on generated code

See original GitHub issue

Using VS2022 og reverse engineer with target "EF Core 6 (preview) and checking option “Split DbContext into Configuration classes” - then the generated code gives compiler error. There is a mixup in which generated code for Configuration class is written to the DbContext class. My generated code fails on:

DbContext - OnModelCreating():

            modelBuilder.ApplyConfiguration(new Configurations.TableNameConfiguration());

            });    // Does not belong here 

            });    // Does not belong here 

            OnModelCreatingPartial(modelBuilder);

And in the corresponding TableConfigurationClass

                entity.HasMany(d => d.ServiceParameter)
                .WithMany(p => p.ProfileParameter)
                .UsingEntity<Dictionary<string, object>>(
                    "ParameterConnection",
                    l => l.HasOne<ServiceParameter>().WithMany().HasForeignKey("ServiceParameterId").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_ParameterConnection_ServiceParameterDefinition"),
                    r => r.HasOne<ProfileParameter>().WithMany().HasForeignKey("ProfileParameterId").HasConstraintName("FK_ParameterConnection_ProfileParameterDefinition"),
                    j =>
                    {
                        j.HasKey("ProfileParameterId", "ServiceParameterId");

                        j.ToTable("ParameterConnection");

                        j.HasIndex(new[] { "ServiceParameterId" }, "IX_ParameterConnection_ServiceParameterId");

            OnConfigurePartial(entity);

In my project this happens on 2 Configuration classes - hence 2 * " });" on DbContext class. This is likely connected to Foreign key or Index definitions.

Correct code should be:

                    j =>
                    {
                        j.HasKey("ProfileParameterId", "ServiceParameterId");

                        j.ToTable("ParameterConnection");

                        j.HasIndex(new[] { "ServiceParameterId" }, "IX_ParameterConnection_ServiceParameterId");
                    });     // Missing in code file - written in DbContext.OnModelCreating method.

            OnConfigurePartial(entity);
        }

Steps to reproduce

Further technical details

EF Core Power Tools version: 2.5.774.0

Database engine: SQL Server

Visual Studio version: Visual Studio 2022 Preview 4.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
ErikEJcommented, Sep 24, 2021

I am working on a non regex based solution.

1reaction
jonnybeecommented, Oct 2, 2021

@ErikEJ yes, works fine for me now 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core 7 Reverse Engineer Error · Issue #1523
Error when reverse engineering a model from an existing DB: Microsoft SQL Server 2014 12.0.6439.10 (X64) using EFCore 7 RC1 System.
Read more >
How do I reverse engineer a compiler?
My inclination would be to disassemble the compiler. If it's written in .NET (C#, VB.NET, etc.) or Java, there are decompilers that will ......
Read more >
Can you "stop" a C program from being reverse engineered?
The real question is, how easy is it to reverse engineer? C is trivial to decompile; I've seen some surprisingly clean C code...
Read more >
EFCorePowerTools
Trying to "Compare DB Context to database" is giving me an error shown below. I have also had issues in the past doing...
Read more >
Strategy for reverse engineering legacy C code (C code)
This topic provides a roadmap to ease the process of reverse engineering legacy code using C Code Reverser, aimed at first time users...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found