简体   繁体   中英

Update entity class in ASP.NET Core Entity Framework

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 ?

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.

To Update entire dbcontext use the below command. link for more details

"Build failed" on Database First Scaffold-DbContext

Scaffold-DbContext -Connection "Server=(local);Database=DefenderRRCart;Integrated Security=True;Trusted_Connection=True;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir RRStoreContext.Models -context RRStoreContext -Project RR.DataAccess -force

To update from Azure Connection and local Connection

Scaffold-DbContext "Server=<Server Name>,1433;Initial Catalog=<Database Name>;Persist Security Info=False;User ID=<user id>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entity -context <Context Name> -Project <project Name> -force

To create the new Context

Scaffold-DbContext "Server=<Server Name>,1433;Initial Catalog=<Database Name>;Persist Security Info=False;
        User ID=<User Id>;Password=<Password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection
        Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir <Dir Name>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM