Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DataSource.twig 2.2KB

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