3

I'm new to .NET Core, and I am exploring this particular tutorial: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-2.2&tabs=visual-studio

Now it seems the Entity Framework takes a code first approach to generate the database tables from the created model, and the tutorial uses this scaffolding thing to create the views and controller before generating the tables via the package manager console.

My question is: I was wondering how does the solution really know which models are to be pushed to the database on Update-Database? I have added some standalone model DBSets to the DBContext manually (in the tutorial MvcMovieContext), but that didn't seem to work. Is it the Migrations folder? How does it work?

I would think that this scaffolding step isn't necessary (especially if I would like to maintain the tables without any UI interfaces), and that it has some kind of list somewhere that says which model object goes into database or not.

1
  • You probably just forgot to run the migration. EF does not create tables from the DbSets directly. You need to run a migration on the DbContext which creates a migration file. EF then build tables on off of that Commented Feb 19, 2019 at 22:11

2 Answers 2

4

Entity Framework (Core) works the following: By creating a DbContext or any class that derives from it, EF Core checks for DbSets and their related classes in the code. Take for example this DbContext:

public class StackOverflowDbContext : DbContext {
    public DbSet<MyClass> Test { get; set; }
}

As soon as you start with your initial migration (can be done for example via dotnet ef migrations add Initial), EF checks for a DbContext class. If there are multiple, you need to specify that, otherwise it takes the first available and analyses it. In this case, the MyClass needs to be added to the database and therefore the class and all it's properties start to appear in the initial migration.

From there, you can update your model whenever you want, but do not forget to create a new migration after that.

I would think that this scaffolding step isn't necessary

And yes, that is true. You don't need to use scaffolding, it's just there for providing a starting point.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation! Another question: If I have to keep adding a new migration each time I have a model change, wouldn't that cause the solution to eventually become very bloated with migration files? Can I put it back to a clean state by: 1. Clearing out the EFMigrationsHistory table. 2. Clearing out the Migration files. 3. Add another migration to consolidate all the things in DbContext?
It could bloat theoretically. However, that shouldn't be a problem as the migration files are kept inside an extra folder, which shouldn't be touched. Even if not recommended unless you are the owner of the database server and the program, you can of course clear the history table, remove all migrations and just create a new "initial" migration.
0

You must to create a migrations and update your db. You can find commands here http://www.entityframeworktutorial.net/efcore/entity-framework-core-migration.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.