Entity Framework Base Database - First Update After Original Forest?

I play with the Entity Framework Core, and I worked on implementing the Database-First application. The original Scaffold-DbContext command works fine and creates all of my objects correctly if it is not organized as we would like. This is a SQL Server database that uses schemas to separate areas of responsibility, and it doesn’t matter to me that Scaffold just drops them all in one folder.

As an aside, I could not determine if there is a way to re-run Scaffold to update classes after updating the database. The closest I can find is to restart the Scaffold-DbContext command with the -force option. However, this also overwrites any user code that I added to the Context.cs file, for example, indicating the connection string to the configuration value instead of hard coding.

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

Is there a flaw in manually coding any future changes for this? Without this, it seems to make the database-based approach completely useless with EF Core.

+9
source share
3 answers

As you said yourself ... The main problem with the first approach to the database: you should never change the model manually, start renaming something, etc. Unless you are 100% sure that your database will no longer change. If you are not 100% sure, just code with the model that was generated automatically.

Re-Scaffolding will overwrite any changes made directly to the model class, deleting everything that you changed or added.

But you can create partial classes on the side that will not be overwritten by automatic mapping:

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

This is a good way to add code to your entity, being sure that it will not be affected by automatic rendering. Just make sure that the partial view has the same name as the automatically generated class, and everything should be in order.

+8
source

Re-runnable scaffold can be used for a true DB First-approach as answered by Antoine Pelletier .

However, most often scaffold is used once to initially import the model into your code, and then continue using the Code First approach. The process of using reverse engineering to create an Entity Framework model based on an existing database is described in https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

+3
source

You can see an example of my code https://github.com/IwanSumpena/AspNetCoreEFDataBaseFirst-Example hope this helps

0
source

Source: https://habr.com/ru/post/1259047/


All Articles