concept database model in category entity framework

appears as: database model, database model
Entity Framework Core in Action

This is an excerpt from Manning's book Entity Framework Core in Action.

In our small example application, all the decisions on the modeling are done by EF Core, which works things out by using a set of conventions. You have loads of extra ways to tell EF Core what the database model is, and these commands can get complex. It takes both chapter 6 and chapter 7 to cover all the options available to you as a developer.

  • Model—This provides access to the database model that EF Core uses when connecting to or creating a database. Section 9.6 covers this topic.
  • Listing 15.23 Capturing the SQL commands EF Core uses to create a database
    [RunnableInDebugOnly]    #1  
    public void CaptureSqlEfCoreCreatesDatabase()
    {
        //SETUP
        var options = this.
            CreateUniqueClassOptions<BookContext>();
        using (var context = new BookContext(options))
        {
            var logs = context.SetupLogging();    #2  
    
            //ATTEMPT
            context.Database.EnsureDeleted();    #3  
            context.Database.EnsureCreated();    #3  
    
            //VERIFY
            foreach (var log in logs.ToList())
            {    #4  
                _output.WriteLine(log.Message);    #4  
            }    #4  
        }
    }
    
    #1   You don’t need this to run every time, so you add the RunnableInDebugOnly attribute so it isn’t run in the normal unit test run.
    #2   Sets up the logging before the database is created
    #3   This combination ensures a new database is created that matches the current EF Core’s database Model.
    #4   Outputs only the Message part of the logging, so you can cut and paste the SQL out of the logged data