.net core使用Mysql数据迁移给【表/表字段】添加注释
程序员文章站
2024-02-02 18:27:46
...
.net core使用Mysql数据迁移给【表/表字段】添加注释
Dependency:
EneityFrameworkCore(ORM)
Pomelo.EntityFrameworkCore.MySql
创建完实体类,DBContext中配置完Fluent Api后,code first使用add-migration生成迁移。
在迁移类的Up()方法中直接使用自定义sql实现表以及表字段的注释,具体如下:
public partial class addCommentEntity : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "comment",
columns: table => new
{
id = table.Column<string>(maxLength: 50, nullable: false),
CreateUser = table.Column<string>(nullable: true),
CreateTime = table.Column<DateTime>(nullable: false),
user_id = table.Column<string>(maxLength: 50, nullable: true),
comments = table.Column<string>(type: "text", nullable: true),
status = table.Column<sbyte>(type: "tinyint", maxLength: 4, nullable: false, defaultValue: (sbyte)0),
LastModified = table.Column<DateTime>(nullable: false),
RowVersion = table.Column<DateTime>(nullable: false),
IsDeleted = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Id", x => x.id);
});
migrationBuilder.Sql(
@"ALTER TABLE comment MODIFY COLUMN user_id VARCHAR(50) NOT NULL COMMENT '用户id';
ALTER TABLE comment MODIFY COLUMN comments text NULL COMMENT '评论留言';
ALTER TABLE comment MODIFY COLUMN status tinyint NOT NULL COMMENT '状态:0 - 所有人可见,1 - 仅好友可见,2 - 仅自己可见'");
}