Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 977
San Jaisy Avatar asked Dec 20 '16 00:12

San Jaisy


People also ask

How do I update my Entity Framework 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 items in Entity Framework?

To update this entity we need to attach the Department to the context and inform it to mark its status as Modified . Now if we call the SaveChanges method, the context will send an update query to the database.

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.


1 Answers

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.

like image 60
Sanket Avatar answered Oct 16 '22 21:10

Sanket