zoukankan      html  css  js  c++  java
  • EF Core MYSQL 生成表映射配置问题

    Model表

        public class Goods
        {
    
            public string ID { get; set; }
    
            public string CreatedBy { get; set; }
            public DateTime CreatedTime { get; set; }
    
            public string ModifiedBy { get; set; }
            
            public DateTime ModifiedTime { get; set; }
    
            public string GoodsName { get; set; }
            public int GoodsNum { get; set; }
        }
    

    配置文件

       public class GoodsEntityTypeConfiguration : IEntityTypeConfiguration<Goods>
        {
            public void Configure(EntityTypeBuilder<Goods> builder)
            {
                builder.ToTable("Sample_Goods_01");
    
                builder.HasKey(ci => ci.ID);
    
                builder.Property(ci => ci.ID).IsRequired();
    
                builder.Property(cb => cb.CreatedBy)
                    .IsRequired()
                    .HasMaxLength(20);
                builder.Property(cb => cb.CreatedTime)
                    .IsRequired();
    
                builder.Property(cb => cb.ModifiedBy)
                    .IsRequired()
                    .HasMaxLength(20);
    
                builder.Property(cb => cb.GoodsName)
                    .IsRequired();
    
                builder.Property(cb => cb.GoodsNum)
                    .IsRequired();
            }
        }
    


    在MYSQL里,想生成ID保存GUID,开始的时候直接生成 string 生成 text
    没有达到我们的效果。ID应该为char(36),CreatedBy 应该为 varchar(20)
    应该改为

       public class GoodsEntityTypeConfiguration : IEntityTypeConfiguration<Goods>
        {
            public void Configure(EntityTypeBuilder<Goods> builder)
            {
                builder.ToTable("Sample_Goods_01");
    
                builder.Property(ci => ci.ID).IsRequired().HasMaxLength(36).HasColumnType("char(36)");
    
                builder.Property(cb => cb.CreatedBy)
                    .IsRequired()
                    .HasMaxLength(20).HasColumnType("varchar(20)");
    
                builder.Property(cb => cb.CreatedTime)
                    .IsRequired();
    
                builder.Property(cb => cb.ModifiedBy)
                    .IsRequired()
                    .HasMaxLength(20).HasColumnType("varchar(20)");
    
                builder.Property(cb => cb.GoodsName)
                    .IsRequired().HasColumnType("varchar(20)");
    
                builder.Property(cb => cb.GoodsNum)
                    .IsRequired();
            }
        }
    

    参考:

    Jeffcky,EntityFramework Core映射关系详解
    MVC - 10.CodeFrist
    蝌蝌,EF6(CodeFirst)+MySql开发脱坑指南

  • 相关阅读:
    Fiddler基础与HTTP状态码
    Fiddler与F12设置代理
    人和机器猜拳游戏
    ng-model 取不到值
    git的使用
    笔记
    INSPIRED启示录 读书笔记
    INSPIRED启示录 读书笔记
    INSPIRED启示录 读书笔记
    INSPIRED启示录 读书笔记
  • 原文地址:https://www.cnblogs.com/tangge/p/10174462.html
Copyright © 2011-2022 走看看