Using POCO with Entity Framework core

salilsingh-9961 351 Reputation points
2023-07-03T19:14:04.6766667+00:00

Hi Team,

I am working on a project using net6.0, Entity framework core (6.0.18). I need to find which approach DB first or code first is used in the project. At the top of All the model classes(including dbcontext class) of the project contains -

// <auto-generated>

// ReSharper disable All

also. there is one file that has .tt extension with it and another file with EF.Reverse.POCO.v3.ttinclude.

As per my current understanding, below are important points -

  1. Looks like entity framework power tools are installed, using which EF Power tools -> Reverse engineer is used to generate the model and dbcontext classes (which are having // <auto-generated> and // ReSharper disable All at top) as when we generate model and dbcontext class using ADO.Net Entity Data Model or using Scaffold-DbContext, // <auto-generated> text never gets generated at top of generated classes. Please let me know if this understanding is correct or not? If not correct please let me know how model and dbcontext class are created? One observation here is using entity framework power tools, model and dbcontext classes are having // <auto-generated> at top but there is no // ReSharper disable All at top. Is it possible for using EntityFramework Reverse POCO Generator to generate model and dbcontext classes also as classes generated by this tool are having // <auto-generated> and // ReSharper disable All at top.
  2. Also as there is a file with tt extension and another with name EF.Reverse.POCO.v3.ttinclude, as per my understanding, these are added downloading/installed using EntityFramework Reverse POCO Generator, which allows such files to be added. It creates POCO files using which select and DML operations are easy to be performed on the database(using model and dbcontext classes already generated) Please let me know if this understanding is correct or not? If not correct please let me know how (and why) classes with .tt extension and EF.Reverse.POCO.v3.ttinclude are created?
  3. Based on my current set of files, please provide me links to show how to perform below operations -
    • If a new column is added to a DB table, how to select that column in select statement, how to insert value in that column using insert statement?
      • If a new table is added in DB, how to select/insert from to in that DB table?
      Please let me know if more information is required on any of above points.

Thanks,

Salil

Developer technologies | .NET | Entity Framework Core
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 82,061 Reputation points Volunteer Moderator
    2023-07-03T19:36:53.86+00:00

    EF core really only supports code first.

    There are scaffolding tools to generate the initial support, but nothing to update with database changes. If you want to stay database first, you just run the scaffolding tools and regenerate the database classes.

    the standard approach after scaffolding, is to switch to database first and migrations. This is known as code first to existing database, and is the expected method.

    it appears you project started database first. If it’s an option, now switch to code first migrations. Check if this has been done by checking for migration files.


  2. Wenbin Geng 746 Reputation points Microsoft External Staff
    2023-07-05T02:59:57.2133333+00:00

    Hi @salilsingh-9961Sorry that our Team does not accept third-party tools for the time being, but if you are simply consulting EF Core, we are very honored to answer for you.

    The easiest way to distinguish between dbfirst and codefirst in ef core is to look at the project structure. The migrated project structure is as follows:

    dbfirst:

    User's image

    codefirst:

    User's image

    If you have more ef core issues, welcome to raise them

    Best Regards,

    Wenbin


    If the answer is helpful, please click "Accept Answer" and upvote it.

    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.  

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.