Benefits of scaffolding an existing database vs. manually specifying only the required properties using fluent API?

Phillip Williams 20 Reputation points
2023-03-14T03:47:55.4066667+00:00

I came upon a project where the original developer coded the entities in EF Core by specifying only the required fields and their navigation properties.

If I scaffold the existing database tables, I get complete definitions of the tables design on the server; including indexes and relationships against other fields that are not needed for this particular application.

Is there any benefit to scaffolding the required tables vs. manually writing the models specs to only indicate the required fields and their navigation properties?

Does it matter if the manually defined models have omitted any mention of the indexes/check constraints that are defined on the server for the specified tables?

I used the dbcontext scaffold command to reverse-engineer the tables that I need but the original developer argued that this is not as efficient. I tend to think that the query performance will be better if I include all the specs as they exist on the server.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,476 Reputation points Microsoft Vendor
    2023-03-14T06:33:23.5266667+00:00

    @Phillip Williams, Welcome to Microsoft Q&A, we need to decide to use scaffold the existing database tables or use Fluent API depend on some specific situations.

    When to to use scaffold the existing database tables:

    a. when you already have the existing database tables, and you need all the indexes and relationships of it

    b. when the table has many fields, and you don't want to make a model class for it.

    c. when you prefer using database set some fields than using code.

    When to use Fluent API.

    a. when you don't have the existing database tables and you have written the model class.

    b. when you have the existing database tables, and you only want to specify fewer fields from the database.

    c. when you prefer using code to set some fields than using database.

    As usual, it means that you chose the code first method in ef core if you use Fluent API.

    In a conclusion,

    if your required properties' count is not too large and you prefer to using code to set these properties, then I recommend that you use Fluent API to do it.

    if your required properties' count is too large and you feel trouble about using code to specify these properties, I suggest that you use scaffold to do it.

    Hope my explanation could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful