In part 4 of the ASP.NET Core MVC tutorial for Visual Studio code, the EF migrations cli command of
在面向Visual Studio代码的ASP.NET核心MVC教程的第4部分中,
dotnet ef migrations add InitialCreate
is producing a migration file with incorrect column data types.
正在生成列数据类型不正确的迁移文件。
After running dotnet ef migrations add InitialCreate
, the command should have taken the following Movie.cs
class:
在运行DotNet Migrations Add InitialCreate之后,该命令应该具有以下Movie.cs类:
using System.ComponentModel.DataAnnotations;
namespace MvcMovie.Models;
public class Movie
{
public int Id { get; set; }
public string? Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string? Genre { get; set; }
public decimal Price { get; set; }
}
And the expected output should have been a migration file Migrations/{time-stamp}_InitialCreate.cs
, with the following content:
预期的输出应该是迁移文件Migrations/{time-stamp}_InitialCreate.cs,包含以下内容:
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MvcMovie.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Movie",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
ReleaseDate = table.Column<DateTime>(type: "datetime2", nullable: false),
Genre = table.Column<string>(type: "nvarchar(max)", nullable: true),
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Movie", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Movie");
}
}
}
The actual output was:
实际产出为:
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MvcMovie.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Movie",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Title = table.Column<string>(type: "TEXT", nullable: true),
ReleaseDate = table.Column<DateTime>(type: "TEXT", nullable: false),
Genre = table.Column<string>(type: "TEXT", nullable: true),
Price = table.Column<decimal>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Movie", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Movie");
}
}
}
As can be seen above, some properties like Price
, produces a column of type TEXT
instead of decimal(18,2)
.
如上所述,一些属性(如Price)会生成一个文本类型的列,而不是DECIMAL(18,2)类型。
更多回答
SQLite and Sql Server are two different DBMS. | SQLite has no decimal type.
SQLite和SQL Server是两种不同的数据库管理系统。|SQLite没有DECIMAL类型。
我是一名优秀的程序员,十分优秀!