F
F
Frip2020-04-23 21:58:08
ASP.NET
Frip, 2020-04-23 21:58:08

How to change the structure of the model (tables) formed using the Entity Framework?

Technologies used
Entity Framework
ASP.NET CORE 3.1

Suppose the following model was initially formed, which was then converted to a table

public class MyModel{
        public Guid Id { get; set; }

        public string Title { get; set; }

        public  string Subtitle { get; set; }

        public  string Text { get; set; }

        public string TitleImagePath { get; set; }
}

This model is also connected to the Users table ( ASP.NET Identity ) through the Guid field

. So, for example, after a while, you need to delete some field from this model and add a couple of new ones. (TOR has changed, changes have been made, etc.)
For example:
public class MyModel{
        public Guid Id { get; set; }

        public string Title { get; set; }

        public  string Text { get; set; }

        public string TitleImagePath { get; set; }

        public int View { get; set; }
      
        public string Tag { get; set;}
}


So here's what you need to do to:
  1. New fields added
  2. Removed required fields
  3. The data that is in the fields that are not deleted has been preserved
  4. Saved data in related tables

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2020-04-23
@mrFrip

Probably you need to read about Migrations .
Added fields must be null or contain a default value. Then, when adding a field, the data will not be deleted.
Deleting fields usually doesn't delete records unless you touch the keys.
The data in the linked tables is not touched if the data is not deleted from the main table and the keys on the basis of which the links are made are not touched.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question