gpt4 book ai didi

EF Migration CLI creates incorrect column data types(EF迁移CLI创建了不正确的列数据类型)

转载 作者:bug小助手 更新时间:2023-10-24 20:41:55 27 4
gpt4 key购买 nike



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类型。

优秀答案推荐

Judging from your autoincement specification, it appears you are using SQLite - this does not support a decimal type, so it will fall back to using TEXT.

从您的自动增强规范来看,您似乎正在使用SQLite--这不支持DECIMAL类型,因此它将退回到使用文本。


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com