using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Linq;
using System.Reflection;

namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL;

public partial class IRCContext : DbContext
{
    public IRCContext()
    {
    }

    public IRCContext(DbContextOptions<IRCContext> options)
        : base(options)
    {
    }

    //public virtual DbSet<TrialImageDownload> TrialImageDownload { get; set; }
    //public virtual DbSet<TestNew> TestNew { get; set; }
    //public virtual DbSet<Subject> Subject { get; set; }
    //public virtual DbSet<SubejectVisit> SubejectVisit { get; set; }
    //public virtual DbSet<Dictionary> Dictionary { get; set; }
    //public virtual DbSet<ReadingClinicalData> ReadingClinicalData { get; set; }
    //public virtual DbSet<ReadModule> ReadModule { get; set; }
    //public virtual DbSet<TestStringLength> TestStringLength { get; set; }



    public virtual DbSet<TestStr> TestStr { get; set; }
    public virtual DbSet<Project> Project { get; set; }

    public virtual DbSet<ProjectUser> ProjectUser { get; set; }
    public virtual DbSet<ProjectUser2> ProjectUser2 { get; set; }

    //public virtual DbSet<TestFieldName> TestFieldName { get; set; }

    //public virtual DbSet<TestComment> TestComment { get; set; }

    //public virtual DbSet<TestStrDefaultValue> TestStrDefaultValue { get; set; }
    

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // 替换默认的 MigrationsSqlGenerator
        optionsBuilder.ReplaceService<IMigrationsSqlGenerator, NoForeignKeyMigrationsSqlGenerator>();
        optionsBuilder.UseSqlServer("Server=106.14.89.110,1433;Database=IRC_Code;User ID=sa;Password=mssql_KnTs2a;TrustServerCertificate=true");


    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //遍历实体模型手动配置
        var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null);
        foreach (var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);
            modelBuilder.ApplyConfiguration(configurationInstance);
        }



        OnModelCreatingPartial(modelBuilder);
    }
    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {

        //configurationBuilder.Conventions.Add(_ => new NoForeignKeyConvention());
        //针对字符串使用默认的长度配置
        configurationBuilder.Conventions.Add(_ => new MaxStringLengthConvention());
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
