選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DataSource.twig 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Microsoft.EntityFrameworkCore;
  2. using Luticate2.Auth.DataAccess.Models;
  3. using Test.Utils.DataAccess.Models;
  4. namespace TestUtils.DataAccess
  5. {
  6. public partial class LuUtilsDbContext : DbContext
  7. {
  8. private readonly string _connectionString;
  9. public LuUtilsDbContext()
  10. {
  11. }
  12. public LuUtilsDbContext(string connectionString)
  13. {
  14. _connectionString = connectionString;
  15. }
  16. public LuUtilsDbContext(DbContextOptions options) :base(options)
  17. {
  18. }
  19. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  20. {
  21. if (_connectionString != null) {
  22. optionsBuilder.UseNpgsql(_connectionString);
  23. }
  24. }
  25. protected override void OnModelCreating(ModelBuilder modelBuilder)
  26. {
  27. {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
  28. modelBuilder.Entity<{{ table.getName() }}>()
  29. .HasKey(c => new { {% for column in table.getPrimaryKeys() %}{% if (column.isSelected()) %}c.{{ column.getName() }}{% if not (loop.last) %}, {% endif %}{% endif %}{% endfor %} });
  30. {% for column in table.getColumns() %}{% if (column.isSelected()) and (column.hasDefaultValue()) %}
  31. modelBuilder.Entity<{{ table.getName() }}>()
  32. .Property(e => e.{{ column.getName() }})
  33. .HasDefaultValueSql("{{ column.getDefaultValueEscaped() }}");
  34. {% endif %}{% endfor %}
  35. {% for fk in table.getSourceForeignKeys() %}
  36. modelBuilder.Entity<{{ fk.getSourceTable().getName() }}>()
  37. .HasOne(e => e.{{ fk.getSourceForeignKeyName() }})
  38. .WithMany(e => e.{{ fk.getTargetForeignKeyName() }})
  39. .HasForeignKey({% for column in fk.getSourceColumns() %}"{{ column.getName }}"{% if not (loop.last) %}, {% endif %}{% endfor %})
  40. .HasConstraintName("{{ fk.getName() }}");
  41. {% endfor %}
  42. {% endif %}{% endfor %}
  43. }
  44. {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
  45. public virtual DbSet<{{ table.getName() }}> {{ table.getName() }} { get; set; }
  46. {% endif %}{% endfor %}
  47. }
  48. }