mysql二进制对应ef中实体表字段类型

1、ef中设计成byte[],如下:

    /// <summary>
    /// 会员证
    /// </summary>
    public class MemberCard : AuditedEntity<Guid>
    {
        /// <summary>
        /// 企业id
        /// </summary>
        public Guid CompanyId { get; set; }

        /// <summary>
        /// 图片地址
        /// </summary>
        public byte[] ImgUrl { get; set; }
    }

使用abp中的dotnet ef migrations add AddTable_MemberCard

生成如下longblob类型

ImgUrl = table.Column<byte[]>(type: "longblob", nullable: false, comment: "证书图片url"),

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BaseService.Migrations
{
    /// <inheritdoc />
    public partial class AddTableMemberCard : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "base_MemberCard",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
                    CompanyId = table.Column<Guid>(type: "char(36)", nullable: false, comment: "企业id", collation: "ascii_general_ci"),
                    ImgUrl = table.Column<byte[]>(type: "longblob", nullable: false, comment: "证书图片url"),
                    CreationTime = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    CreatorId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"),
                    LastModificationTime = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    LastModifierId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_base_MemberCard", x => x.Id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "base_MemberCard");
        }
    }
}

那咋样才能生成Blob类型的字段呢,其实可以在ef中使用HasColumnType配置字段类型,如下:

            builder.Entity<MemberCard>(b =>
            {
                b.ToTable("base_MemberCard");
                b.ConfigureByConvention();

                b.Property(t => t.CompanyId).HasColumnName("CompanyId").HasComment("企业id").IsRequired();
                b.Property(t => t.ImgUrl).HasColumnName("ImgUrl").HasComment("证书图片url").HasColumnType("Blob").IsRequired();
            });

生成的

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BaseService.Migrations
{
    /// <inheritdoc />
    public partial class AddTableMemberCard : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "base_MemberCard",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
                    CompanyId = table.Column<Guid>(type: "char(36)", nullable: false, comment: "企业id", collation: "ascii_general_ci"),
                    ImgUrl = table.Column<byte[]>(type: "Blob", nullable: false, comment: "证书图片url"),
                    CreationTime = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    CreatorId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"),
                    LastModificationTime = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    LastModifierId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_base_MemberCard", x => x.Id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "base_MemberCard");
        }
    }
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吱吱喔喔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值