1

I have scaffolded my database into models using the following command:

dotnet ef dbcontext scaffold "Server=.\;Database=MyApp;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models

Whenever there is a db change I fire the above command with the -force flag.

This has auto generated the dbcontext and class files representing the tables into the models folder.

My question:

In the dbcontext.cs file, in addition to the class constructor and dbset, there are the following methods generated:

  1. OnModelCreating
  2. OnModelCreatingPartial

What is the purpose of these methods and can I get rid of them.

For example: In the program.cs I am planning to add:

builder.Services.AddDbContext<MyAppContext>(opt =>
    opt.UseSQLServer(".\;Database=MyApp;Trusted_Connection=True;"));

So I can remove the OnConfigurimg method. What about the above 2 methods?

I want to create some models, for example additional tables and identify tables that I want to migrate into the database. So I'm confused with how to proceed due to the presence of those functions.

variable
  • 8,262
  • 9
  • 95
  • 215
  • It's *generated code*. Just settle with what it generates, as with so many code generators out there. The purpose of these methods is not different than any override or partial method. – Gert Arnold Jan 08 '22 at 08:55
  • But what role does it play during project debug? I'm trying to understand what is its purpose. – variable Jan 08 '22 at 10:00
  • After the db first approach, I want to create some models that I want to migrate into the database. So I'm confused with how to proceed due to the presence of those functions. – variable Jan 08 '22 at 15:19
  • https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext.onmodelcreating?view=entity-framework-6.2.0 – phuzi Jan 08 '22 at 15:28
  • *I want to create some models that I want to migrate into the database* Maybe you should explain what you intend do do there, that would give the question more context and meaning. – Gert Arnold Jan 08 '22 at 15:34
  • I mean I want to add additional tables and also identity tables. – variable Jan 08 '22 at 15:39
  • Yeah, but that's too vague, or too much. Try to add an example, like: this is what I've got and I want to add these two or three classes, this is what I did and now I don't understand ... . Nevertheless, I consider [this](https://stackoverflow.com/q/52182040/861716) a duplicate. – Gert Arnold Jan 08 '22 at 18:47

1 Answers1

1

Instead of building models matching tables in your database manually, scaffolding helps you to map your database into models to work within your project. This is part of database-first development.

Once you have the models, you can then extend your context in your project with new entities, create new data, make migrations and expand your database by your project. This way you will break the database-first approach and start a mixed development.

These two methods exist so you can extend your model by adding new entities to your context. you will find lots of examples about using them.

You may also have other initializations besides, and/or instead of, adding new things within these methods

as for the removal, your context is inherited from DbContext where these are defined as virtual, so your context file must have at least empty implementations of them.

the official documentation for OnModelCreating mentions this:

This method is called when the model for a derived context has been initialized, but before the model has been locked down and used to initialize the context. The default implementation of this method does nothing, but it can be overridden in a derived class such that the model can be further configured before it is locked down

Yılmaz Durmaz
  • 2,374
  • 12
  • 26
  • When I put a debugger inside this method and run the project, then the debugger is hit. Why is that? Isn't that method only used by migrations? – variable Jan 14 '22 at 19:01
  • @variable, this class is your main context for everything related to your database operations, including migrations. you may replicate its content in a new class and use that as your context. but these methods will always run to check if you have anything else. plus most of the examples you will see add new models through them. – Yılmaz Durmaz Jan 14 '22 at 19:09