using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.EntityFrameworkCore; namespace IRaCIS.Core.Test.CodeFirstTest.PGSQL.Migrations; public abstract class BaseFullAuditEntity { [Key] public Guid Id { get; set; } public Guid CreateUserId { get; set; } public DateTime CreateTime { get; set; } public Guid UpdateUserId { get; set; } public DateTime UpdateTime { get; set; } } public class TestStr : BaseFullAuditEntity { public string DefualtLength { get; set; } [MaxLength] public string MaxLength { get; set; } [StringLength(400)] public string DefineLength { get; set; } } public partial class PGContext : DbContext { public PGContext() { } public PGContext(DbContextOptions options) : base(options) { } public virtual DbSet TestStr { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql("Host=106.14.89.110;Port=5432;Username=sa;Password=pgsql_pwd;Database=Test6_PG"); protected override void OnModelCreating(ModelBuilder modelBuilder) { if (base.Database.IsNpgsql()) { modelBuilder.HasPostgresExtension("uuid-ossp"); //保证pgsql 生成的时间默认为timestamp 而不是 timestamp with time zone foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { foreach (var property in entityType.GetProperties()) { if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?)) { property.SetColumnType("timestamp without time zone"); } } } } OnModelCreatingPartial(modelBuilder); } protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) { //configurationBuilder.Conventions.Add(_ => new NoForeignKeyConvention()); //针对字符串使用默认的长度配置 configurationBuilder.Conventions.Add(_ => new MaxStringLengthConvention()); } partial void OnModelCreatingPartial(ModelBuilder modelBuilder); }