C# – Update entity class in ASP.NET Core Entity Framework

asp.net-corecentity-framework-core

I have created the model from existing database using Entity Framework in ASP.NET Core.

Here is the model of the Market table

public partial class Market
{
        public Guid MarketId { get; set; }
        public string City { get; set; }
        public string CityF { get; set; }
        public string Name { get; set; }
        public string NameF { get; set; }
        public int SortOrder { get; set; }
        public bool IsActive { get; set; }
}

However, I have change the MarketId datatype to int in the database. Now I want to update the model.

Found some link but this link create the entire model again https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

How can I update the model without the creating new model and using the connection string in appsettings.json?

Best Answer

One option is-

You can use Scaffold-DbContext command with -force flag. This way you can force scaffolding to overwrite existing model files.

sample command -

Scaffold-DbContext "<ConnectionString>" Microsoft.EntityFrameworkCore.SqlServer -t <tablename> -f

Replace ConnectionString & TableName as per your requirements.