Change or rename a column name without losing data with Entity Framework Core 2.0

EF Core creates its migrations by comparing your models to the current database snapshot (a c# class). It then uses this to create a migration file you can review. However, EF Core cannot always know if you replaced this column or created a new column. When you check your migration file, make sure there are … Read more

Entity Framework Core migration – connection string

All the examples I’ve seen involve either hard-coding the connection string or putting it in my ASP.NET Core application’s settings files. If you aren’t using ASP.NET Core, or maybe, I don’t know, don’t want to have your local environment’s database details committed to source control, you can try using a temporary environment variable. First, implement … Read more

Update-Database fails due to Pending Changes, but Add-Migration Creates a Duplicate Migration

This answer explains why it happens. To resolve it I call add-migration and name it MERGE and then remove any duplicate migration code that has already happened. This is just to update the model snapshot to reflect the merged model. Example: public partial class MERGE : DbMigration { public override void Up() { // Intentionally … Read more

Adding CreatedDate to an entity using Entity Framework 5 Code First

Here is how I did it: [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime CreatedDate{ get; set; } in my migration’s Up() method: AddColumn(“Agents”, “CreatedDate”, n => n.DateTime(nullable: false, defaultValueSql: “GETUTCDATE()”));

Entity Framework migrations stopped detecting the POCO updates

This may be in two reasons: There is some other DbContext in code, that’s why automatic migrations could not decide, which context to use. There is some new change, which loops a comparison of schema and code model, so EF simply could not find the difference. In general, automatic migrations are simple and fast to … Read more

update the database from package manager console in code first environment

You can specify connection string via ConnectionString parameter: Update-Database -ConnectionString “data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework” -ConnectionProviderName “System.Data.SqlClient” -Verbose Also you need to use this parameter with the same value for Add-Migration command: Add-Migration Version_Name -ConnectionString “data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework” -ConnectionProviderName “System.Data.SqlClient” -Verbose

Command line connection string for EF core database update

In EF Core 5.0, you will pass the connection string in the command line like this, dotnet ef database update –connection “connection string” Reference: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#new-command-line-parameters-for-namespaces-and-connection-strings