Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core Database-First Update after initial Scaffold?

I'm playing around with Entity Framework Core and I have been working on implementing a Database-First application. The initial Scaffold-DbContext command works just fine and creates all my entities correctly, if not organized as I would like. It's a SQL Server database that uses schemas to break up areas of responsibility and I don't really care for the fact that the Scaffold just throws them all into a single folder.

That aside, I have been unable to determine if there is a way to re-run the Scaffold to update the classes after a database update has occurred. The closest I can find is to re-run the Scaffold-DbContext command with the -force parameter. However, this also overwrites any custom code I have added to the Context.cs file, like pointing the connection string to a config value instead of hard-coding.

I have looked at a couple other questions similiar to this one, but it only talks about the initial scaffold, not further updates.

Is there a way short of manually coding any future changes to do this? Without that it seems to make a database-first approach utterly worthless with EF Core.

like image 388
BBlake Avatar asked Oct 31 '16 14:10

BBlake


People also ask

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

How do I update Entity Framework model from database first in .NET core?

Updating the Model If you need to re-scaffold the model after database schema changes have been made, you can do so by specifying the -f or --force option e.g.: dotnet ef dbcontext scaffold "Server=. \;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft. EntityFrameworkCore.

How do I update a record in Entity Framework Core?

First, we create a new context and retrieve the existing department data from the database. We modify the Descr field. Since the context is open, it will mark the entity as Modified . Finally, when we call the SaveChanges , the context generates the update SQL statement to persist the change to the database.


1 Answers

Like you said yourself... The main problem with database first approach : You should never change the model manually and start renaming things etc. Except if you are 100 % sure that your database won't change anymore. If you'r not 100 % sure, just code with the model that has been auto-generated.

Re-Scaffolding will overwrite any changes made directly in the model class, erasing all what you have changed or added.

But you can make partial classes on the side that won't be overwritten by auto mapping :

public partial class TableName
{
    public string Name_of_a_property
    {get; set;}
}

It's a nice way to add code to your entity while being sure it won't be touched by auto-mapping. Just make sure the partial view has the same name as the auto-generated class and everything should be OK.

like image 85
Antoine Pelletier Avatar answered Sep 23 '22 06:09

Antoine Pelletier