Update entity class in ASP.NET Core Entity Framework
Asked Answered
D

2

36

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://learn.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?

Deferred answered 20/12, 2016 at 0:49 Comment(9)
You can use Scaffold-DbContext command with -force flag. This way you can force scaffolding to overwrite existing model files.Geller
why not just change MarketId to int?Equilibrist
Manually changing is not the best option.Deferred
@sanket - can you please provide me the exampleDeferred
@sanjaisy this is sample command - Scaffold-DbContext "<ConnectionString>" Microsoft.EntityFrameworkCore.SqlServer -t <tablename> -f replace ConnectionString & tablenameGeller
Also note, you will need to do this anytime you make a change in the DB like column added or removed or changed... When you force a scaffold update you will loose any modification you have made to the model file not that I can think of why anyone would do that.Sublieutenant
@sanjaisy Did u tried with Scaffold-DbContext command?Geller
@Geller - Yes it worked thanksDeferred
@sanjaisy great :) but if you come across any other option please do share here.Geller
D
29

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>
Deferred answered 1/1, 2017 at 4:10 Comment(3)
How is this the correct answer? The questions is about updating a single Entity, the answer updates the entire dbContext.Unwatched
@Unwatched - Yes, you are correct it doesn't update the single entity It just replaces the single entity along with other entities.Deferred
Looks like your initial question is tracked as a feature request for EF here github.com/aspnet/EntityFrameworkCore/issues/831 Maybe you want to add this to your answer?Unwatched
G
47

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.

Geller answered 21/12, 2016 at 4:47 Comment(4)
This really helped ThanksLocular
This also works in ASP.NET Core 2.0. It will automatically generate a context file within the project root, if the context file is elsewhere. However, generated code for the new table can easily be moved from OnModelCreating and the extra context removed.Analyze
How to do the same programmatically?Immunology
@SagarPatil You can auto generate a context with: Scaffold-DbContext "Server=tcp:"SomeAzureConnectionString";Initial Catalog=(YoutDataBaseName);User ID=(YourUser);Password=(YourPassword);Trusted_Connection=False;Encrypt=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -fAcrogen
D
29

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>
Deferred answered 1/1, 2017 at 4:10 Comment(3)
How is this the correct answer? The questions is about updating a single Entity, the answer updates the entire dbContext.Unwatched
@Unwatched - Yes, you are correct it doesn't update the single entity It just replaces the single entity along with other entities.Deferred
Looks like your initial question is tracked as a feature request for EF here github.com/aspnet/EntityFrameworkCore/issues/831 Maybe you want to add this to your answer?Unwatched

© 2022 - 2024 — McMap. All rights reserved.