Lỗi consider using code first migrations to update the database năm 2024

There is going to come a point where you will want to update your existing database model from your code. The typical way to do this is to use migrations; however, using Code First with an existing database requires a little extra care to work correctly.

First of all, you must enable migrations for the project. In Package Manager Console, type the following command (we need to specify the context to use because we included authentication in the project and this uses its own context by default):

Enable-Migrations -ContextTypeName CodeFirstExistingDB.StoreContext

This will create a Migrations folder and add a Configuration.cs file to it. Next we want to create our migrations to run. This is where you need to add an extra step for an existing database. If we create a migration now, it will attempt to add all our entities to the database. This will not work because the products and categories tables already exist in the database, so we need to create an initial blank migration and then later we will be able to add a migration for any new changes. To create an initial blank migration for the database, type the following command into Package Manager Console:

Add-Migration InitialCreate -IgnoreChanges

The key part of this command is the -IgnoreChanges flag, which ensures that a migration is created that effectively does nothing. Running it will add an entry to the migrations table in the database, thus creating a snapshot of its original schema.

Next, run the update-database command in order to update the existing database with the initial migration. A new migrations table will now have been created in the CodeFirstFromExistingDB database.

Following this, add a new property named Description to the Product class, with a maximum allowed length of 50 characters, as follows:

namespace CodeFirstExistingDB { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Spatial;

public partial class Product { public int Id { get; set; }

[Required] [StringLength(50)] public string Name { get; set; }

[StringLength(50)] public string Description { get; set; }

public int? CategoryID { get; set; } public virtual Category Category { get; set; } } }

Now add a new migration for the product Description field so you can add it as a new column to the Products table. You do this by typing the following command in the Package Manager Console:

Add-Migration add_product_description

A new code file will be produced in the Migrations folder that will include code to add a description column to the Products table as follows:

namespace CodeFirstExistingDB.Migrations { using System; using System.Data.Entity.Migrations;

public partial class add_product_description : DbMigration { public override void Up() { AddColumn("dbo.Products", "Description", c => c.String(maxLength: 50)); }

public override void Down() { DropColumn("dbo.Products", "Description"); } } }

Now run the update-database command in Package Manager Console to update the database. The new description column will be added to the Products table, as shown in the figure below:

The new Description column in the Products table of the CodeFirstFromExistingDB database

Now add some test data to the database. View the data of the Products table via SQL Server Object Explorer and enter some descriptions, as shown below:

Chủ đề