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

Add ability to not generate M2M scaffolding with EF Core 6 #1184

Closed
ErikEJ opened this issue Nov 16, 2021 · 11 comments
Closed

Add ability to not generate M2M scaffolding with EF Core 6 #1184

ErikEJ opened this issue Nov 16, 2021 · 11 comments

Comments

@ErikEJ
Copy link
Owner

ErikEJ commented Nov 16, 2021

https://github.com/dotnet/efcore/blob/fb61eb7cfb75253fbae93aeac23c404ba68b83a8/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs#L658-L660

Override and bypass whole code to detect M2M.

@janseris
Copy link
Sponsor

Maybe a combination of the current EF Core 6 state with option "also explicitly generate join tables" would be nice

@ErikEJ
Copy link
Owner Author

ErikEJ commented Nov 25, 2021

Yes, this is what this feature is about!

@ErikEJ ErikEJ removed this from the Backlog milestone Nov 26, 2021
@ErikEJ ErikEJ closed this as completed in 7831b6f Nov 28, 2021
@ErikEJ
Copy link
Owner Author

ErikEJ commented Nov 28, 2021

This is now available as an advanced option in the latest daily build

@Timothy-Siford
Copy link

I am not entirely sure where to ask this question so I apologize if this is the wrong location.

I want to keep the M2M scaffolding so that I don't have to work with the middle/translational entity. Awesome. Simply keep the option entitled "Use many to many entity (EF Core 6)" under Advanced... as not checked. That said, when choosing this approach, I am presented with generated code that does not build unless I change it. Here is a very simple example to demonstrate my point.

User Table with Id, Name
Role Table with Id, Name
UserRole Table with UserId, RoleId (UserId being a FK to User.Id and RoleId being an FK to Role.Id)

When generating the code I get the following at the end of the generated User.cs file ...
[ForeignKey(nameof(UserId))]
[InverseProperty(nameof(Role.Users))]
public virtual ICollection Roles { get; set; }

The line [ForeignKey(nameof(UserId))] does not recognize UserId (the normal red squiggly line in the Visual Studio IDE with the requisite message being "The name 'UserId' does not exist in the current context" . However, if I change the entire line manually to [ForeignKey("UserId")] then everything builds with no issue.

The same is true for the Role table. When generating the code I get the following at the end of the generated Role.cs file ...
[ForeignKey(nameof(RoleId))]
[InverseProperty(nameof(User.Roles))]
public virtual ICollection Users { get; set; }

The line [ForeignKey(nameof(RoleId))] does not recognize RoleId.

What am I missing here? I feel like there is probably some simple configuration or setup that I am missing that will allow the scaffolding to work as designed.

@janseris
Copy link
Sponsor

janseris commented Nov 1, 2022

I am not entirely sure where to ask this question so I apologize if this is the wrong location.

I want to keep the M2M scaffolding so that I don't have to work with the middle/translational entity. Awesome. Simply keep the option entitled "Use many to many entity (EF Core 6)" under Advanced... as not checked. That said, when choosing this approach, I am presented with generated code that does not build unless I change it. Here is a very simple example to demonstrate my point.

User Table with Id, Name Role Table with Id, Name UserRole Table with UserId, RoleId (UserId being a FK to User.Id and RoleId being an FK to Role.Id)

When generating the code I get the following at the end of the generated User.cs file ... [ForeignKey(nameof(UserId))] [InverseProperty(nameof(Role.Users))] public virtual ICollection Roles { get; set; }

The line [ForeignKey(nameof(UserId))] does not recognize UserId (the normal red squiggly line in the Visual Studio IDE with the requisite message being "The name 'UserId' does not exist in the current context" . However, if I change the entire line manually to [ForeignKey("UserId")] then everything builds with no issue.

The same is true for the Role table. When generating the code I get the following at the end of the generated Role.cs file ... [ForeignKey(nameof(RoleId))] [InverseProperty(nameof(User.Roles))] public virtual ICollection Users { get; set; }

The line [ForeignKey(nameof(RoleId))] does not recognize RoleId.

What am I missing here? I feel like there is probably some simple configuration or setup that I am missing that will allow the scaffolding to work as designed.

Does this just mean that you ticked the option to generate annotations and they are wrongly generated (by EF Power Tools)?

@ErikEJ
Copy link
Owner Author

ErikEJ commented Nov 1, 2022

@Timothy-Siford Please create a new issue and provide a sample SQL script, and/or a repro project (incl SQL script) and I will have a look.

@Timothy-Siford
Copy link

Does this just mean that you ticked the option to generate annotations and they are wrongly generated (by EF Power Tools)?

Thank you!!! That looks exactly to be the issue. With DataAnnotations checked off then it generates incorrectly.

@ErikEJ
Copy link
Owner Author

ErikEJ commented Nov 1, 2022

@Timothy-Siford which EF Core version?

@Timothy-Siford
Copy link

My teammate decided to jump on this and discovered that it's a combination of enabling both Data Annotations and Handlebars (C#).

DataAnnotations adds the [ForeignKey("UserId")]
Handlebars (C#) changes this to [ForeignKey(nameof(UserId))]

A member of our team had added the Handlebars option awhile ago in preparation for something that was never pursued on this project. When we turn handlebars off, we get [ForeignKey("UserId")] which works perfectly.

My teammate does have a test project that shows the issue ... but given that we don't need handlebars, we can turn that off and be good to go.

@Timothy-Siford
Copy link

Oh! We're using EF Core v6.0.9

@nathanpovo
Copy link

In case anyone else was trying to find how to use this feature, the option to enable this is called "Use many to many entity"

image

For the CLI version, set the use-many-to-many-entity option to true

image

Link to the documentation https://github.com/ErikEJ/EFCorePowerTools/wiki/Reverse-Engineering#code-generation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants