您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DataSource.twig 1.7KB

1234567891011121314151617181920212223242526272829303132333435
  1. using Microsoft.EntityFrameworkCore;
  2. using WebApiWebSem.DataAccess.Models;
  3. namespace WebApiWebSem.DataAccess
  4. {
  5. public partial class WsDbContext : DbContext
  6. {
  7. public WsDbContext(DbContextOptions options) :base(options)
  8. {
  9. }
  10. protected override void OnModelCreating(ModelBuilder modelBuilder)
  11. {
  12. {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
  13. modelBuilder.Entity<{{ table.getName() }}>()
  14. .HasKey(c => new { {% for column in table.getPrimaryKeys() %}{% if (column.isSelected()) %}c.{{ column.getName() }}{% if not (loop.last) %}, {% endif %}{% endif %}{% endfor %} });
  15. {% for column in table.getColumns() %}{% if (column.isSelected()) and (column.hasDefaultValue()) %}
  16. modelBuilder.Entity<{{ table.getName() }}>()
  17. .Property(e => e.{{ column.getName() }})
  18. .HasDefaultValueSql("{{ column.getDefaultValueEscaped() }}");
  19. {% endif %}{% endfor %}
  20. {% for fk in table.getSourceForeignKeys() %}
  21. modelBuilder.Entity<{{ fk.getSourceTable().getName() }}>()
  22. .HasOne(e => e.{{ fk.getSourceForeignKeyName() }})
  23. .WithMany(e => e.{{ fk.getTargetForeignKeyName() }})
  24. .HasForeignKey({% for column in fk.getSourceColumns() %}"{{ column.getName }}"{% if not (loop.last) %}, {% endif %}{% endfor %})
  25. .HasConstraintName("{{ fk.getName() }}");
  26. {% endfor %}
  27. {% endif %}{% endfor %}
  28. }
  29. {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
  30. public virtual DbSet<{{ table.getName() }}> {{ table.getName() }} { get; set; }
  31. {% endif %}{% endfor %}
  32. }
  33. }