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

Scaffolding API endpoints with primary key named {class}Id creates incorrect code #2576

Open
christiannagel opened this issue Nov 9, 2023 · 4 comments

Comments

@christiannagel
Copy link

Using Visual Studio to scaffold API with read/write endpoints, using Entity Framework creates incorrect code if the model class has a primary key not named with Id, but with {class}Id (this is a convention with EF Core to create primary keys).

Using this class:

public class Book
{
    public int BookId { get; set; }
    public required string Title { get; set; }
    public string? Publisher { get; set; }
}

Selecting these options with scaffolding:

  • existing Book class
  • Create a new endpoints class
  • Create a new DbContext class
  • SQL Server database provider
  • Use OpenAPI
  • Use TypedResults

Creates this code:

        group.MapGet("/{id}", async Task<Results<Ok<Book>, NotFound>> (int bookid, BooksContext db) =>
        {
            return await db.Book.AsNoTracking()
                .FirstOrDefaultAsync(model => model.BookId == bookid)
                is Book model
                    ? TypedResults.Ok(model)
                    : TypedResults.NotFound();
        })
        .WithName("GetBookById")
        .WithOpenApi();

bookid was correctly found with the generated code. However, with the route, incorrectly, id is used. Here I would expect bookid to match it with the parameter, or id with the parameter.
The same issue is with the MapPut and MapDelete methods.

See this repo with the generated code:
https://github.com/christiannagel/issues-scaffoldingapi/tree/main/PrimaryKey

Versions:

Visual Studio 17.8.0 Preview 6
.NET 8.0.100-rc2.23502.2

@deepchoudhery
Copy link
Contributor

yes that would make sense. Will fix this soon.

@julielerman
Copy link

julielerman commented Dec 11, 2023

Can I sneak in a request to also change a few other things in this template things while you're at it?

  1. GetAll should use AsNoTracking
  2. Getall could also benefit from a warning comment that an unchecked GetAll query could create all kinds of performance issues.
  3. put should not be updating the key property :)

If this needs a new issue, let me know . Thanks!

@deepchoudhery
Copy link
Contributor

Can I sneak in a request to also change a few other things in this template things while you're at it?

  1. GetAll should use AsNoTracking
  2. Getall could also benefit from a warning comment that an unchecked GetAll query could create all kinds of performance issues.
  3. put should not be updating the key property :)

If this needs a new issue, let me know . Thanks!

Hey would you mind opening a new issue for tracking this; I like to keep issues separated sorry.

Thanks,
Deep

@julielerman
Copy link

Not at all! I was being a little lazy so I could move ahead with my project. Will get it to you soon. Thanks!!

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

No branches or pull requests

3 participants