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 in EF Core 6 not generating class for a table (works in EF Core 3)

See original GitHub issue

The class for the table dbo.GunnaUserRole is not being genrated in EF Core 6 (is being generated in EF Core 3) . I’m using SQL Server.

Tables:

CREATE TABLE [dbo].[GunnaRole](
	[RoleId] [int] IDENTITY(1,1) primary key,
	[RoleName] [nvarchar](50) NULL)

CREATE TABLE [dbo].[GunnaUser](
	[UserId] [int] IDENTITY(100,1) primary key,
	[NationalId] [varchar](10) NULL)

CREATE TABLE [dbo].[GunnaUserRole](
	[RoleId] [int] NOT NULL,
	[UserId] [int] NOT NULL,
	CONSTRAINT PK_GunnaUserRole PRIMARY KEY (RoleId,UserId),
	FOREIGN KEY (RoleId) REFERENCES dbo.GunnaRole(RoleId),
	FOREIGN KEY (UserId) REFERENCES dbo.GunnaUser(UserId)
)

The generated class (in EF Core 3):

namespace MyAPI.Databases
{
    [Table("GunnaUserRole", Schema = "dbo")]
    public partial class GunnaUserRole
    {
        [Key]
        public int RoleId { get; set; }
        [Key]
        public int UserId { get; set; }

        [ForeignKey(nameof(RoleId))]
        [InverseProperty(nameof(GunnaRole.GunnaUserRole))]
        public virtual GunnaRole Role { get; set; }
        [ForeignKey(nameof(UserId))]
        [InverseProperty(nameof(GunnaUser.GunnaUserRole))]
        public virtual GunnaUser User { get; set; }
    }
}

I’m using handlebars.

Could it happen that this is a changed behavior? In EF Core 6 the Role property is now in the GunnaUser Class (it was before in the Class GunnaUserRole above):

namespace MyAPI.Databases
{
    [Table("GunnaUser", Schema = "dbo")]
    public partial class GunnaUser
    {
        public GunnaUser()
        {
            Role = new HashSet<GunnaRole>();
        }

        [Key]
        public int UserId { get; set; }
        [StringLength(10)]
        [Unicode(false)]
        public string NationalId { get; set; }

        [ForeignKey("UserId")]
        [InverseProperty("User")]
        public virtual ICollection<GunnaRole> Role { get; set; }
    }
}

Thanks, Gunnar

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ErikEJcommented, Feb 16, 2023

@GearTheWorld Correct. And change the target framework in your. Csproj

0reactions
GearTheWorldcommented, Feb 16, 2023

Oh I see now I understand. I have to go in Nuget Packages and update all

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scaffolding (Reverse Engineering) - EF Core
Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema.
Read more >
Reverse Engineering Existing Databases with Entity ...
NET developer to do when tasked with integrating and extending existing databases in their Entity Framework Core projects? In this short post, ...
Read more >
Entity Framework Core, custom changes and preventing ...
I can run the EF Core Power Tools 'Reverse Engineering' option, refresh my table entities, but not lose any custom data type changes....
Read more >
Detect simple join tables in reverse engineering and create ...
The pri-zero here is that a database created by EF6 with many-to-many relationships should reverse-engineer to EF Core with many-to-many ...
Read more >
Generating a model from an existing database
In this approach, you reverse-engineer a model from an existing database, resulting in the generation of an EDMX file that contains the model ......
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