public MappingProfile() { AllowNullCollections = true; CreateMap <string, CronExpression>().ConvertUsing(s => new CronExpression(s)); CreateMap <CronExpression, string>().ConvertUsing(s => s.Cron); CreateMap <string, ExecutionState>().ConvertUsing(s => ExecutionState.FromName(s, true)); CreateMap <ExecutionState, string>().ConvertUsing(s => s.Name); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { // add hangfire models modelBuilder.OnHangfireModelCreating(); var authComparer = new ValueComparer <TokenAuth>((a, b) => a != b, auth => auth.GetHashCode()); modelBuilder.Entity <Cronjob>( builder => { builder.Property(e => e.ProjectId).IsRequired(); builder.HasOne(e => e.Template).WithMany().OnDelete(DeleteBehavior.SetNull); builder.Property(e => e.Cron) .HasConversion(val => val.Cron, dbVal => new CronExpression(dbVal)); builder.Property(e => e.Auth) .HasJsonConversion(authComparer) .HasColumnType("JSONB"); } ); modelBuilder.Entity <Project>(builder => { builder.HasIndex(p => p.Title).IsUnique(); builder.HasIndex(e => new { e.Title, e.Version }).IsUnique(); builder.HasOne(e => e.Template).WithMany().OnDelete(DeleteBehavior.SetNull); builder.Property(e => e.Auth) .HasJsonConversion(authComparer) .HasColumnType("JSONB"); }); modelBuilder.Entity <ConfigTemplate>(builder => { builder.ToTable("config_templates"); builder.HasIndex(e => e.Key).IsUnique(); builder.Property(e => e.Auth) .HasJsonConversion(authComparer) .HasColumnType("JSONB"); }); modelBuilder.Entity <ExecutionStatus>( builder => { var comparer = new ValueComparer <Dictionary <string, object> >( (d1, d2) => d1.SequenceEqual(d2), d => d.Aggregate(0, (agg, val) => HashCode.Combine(agg, val.GetHashCode())), d => d ); builder.Property(e => e.Details) .HasJsonConversion(comparer); builder.Property(e => e.State) .HasConversion( val => val.Name, dbValue => ExecutionState.FromName(dbValue, true) ); } ); modelBuilder.Entity <User>(builder => { builder.ToTable("Users"); builder.Property(e => e.Name).IsRequired(); builder.Property(e => e.Email).IsRequired(); builder.HasIndex(e => e.Email).IsUnique(); builder .HasMany(e => e.Roles) .WithMany(r => r.Users) .UsingEntity <Dictionary <string, object> >( "UserRole", lookup => lookup.HasOne <Role>().WithMany().HasForeignKey("RoleId"), lookup => lookup.HasOne <User>().WithMany().HasForeignKey("UserId") ); builder.HasMany <UserClaim>(e => e.Claims).WithOne(e => e.User); }); modelBuilder.Entity <Role>(builder => { builder.ToTable("Roles"); builder.Property(e => e.Name).IsRequired(); builder.HasIndex(e => e.Name).IsUnique(); }); modelBuilder.Entity <UserClaim>(builder => { builder.ToTable("UserClaims"); builder.Property(e => e.UserId).IsRequired(); builder.Property(e => e.ClaimType).IsRequired(); builder.Property(e => e.ClaimValue).IsRequired(); builder.HasIndex(e => e.ClaimType); }); modelBuilder.ConfigureTimestamps(); modelBuilder.ApplyNamingConventions(); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { // add hangfire models modelBuilder.OnHangfireModelCreating(); modelBuilder.Entity <Cronjob>( builder => { builder.Property(e => e.ProjectId).IsRequired(); builder.Property(e => e.Cron) .HasConversion(val => val.Cron, dbVal => new CronExpression(dbVal)); } ); modelBuilder.Entity <Project>(builder => { builder.HasIndex(p => p.Title).IsUnique(); builder.HasIndex(e => new { e.Title, e.Version }).IsUnique(); }); modelBuilder.Entity <ExecutionStatus>( builder => { builder.Property(e => e.Details).HasConversion( val => JsonSerializer.Serialize(val, null), dbVal => JsonSerializer.Deserialize <Dictionary <string, object> >(dbVal, null) ); builder.Property(e => e.State).HasConversion( val => val.Name, dbValue => ExecutionState.FromName(dbValue, true) ); } ); modelBuilder.Entity <User>(builder => { builder.ToTable("Users"); builder.Property(e => e.Name).IsRequired(); builder.Property(e => e.Email).IsRequired(); builder.HasIndex(e => e.Email).IsUnique(); builder .HasMany(e => e.Roles) .WithMany(r => r.Users) .UsingEntity <Dictionary <string, object> >( "UserRole", m2m => m2m.HasOne <Role>().WithMany().HasForeignKey("RoleId"), m2m => m2m.HasOne <User>().WithMany().HasForeignKey("UserId") ); builder.HasMany <UserClaim>(e => e.Claims).WithOne(e => e.User); }); modelBuilder.Entity <Role>(builder => { builder.ToTable("Roles"); builder.Property(e => e.Name).IsRequired(); builder.HasIndex(e => e.Name).IsUnique(); }); modelBuilder.Entity <UserClaim>(builder => { builder.ToTable("UserClaims"); builder.Property(e => e.UserId).IsRequired(); builder.Property(e => e.ClaimType).IsRequired(); builder.Property(e => e.ClaimValue).IsRequired(); builder.HasIndex(e => e.ClaimType); }); modelBuilder.ConfigureTimestamps(); modelBuilder.ApplyNamingConventions(); }