| | 1 | | using Domain.Entities; |
| | 2 | | using Microsoft.EntityFrameworkCore; |
| | 3 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | 4 | |
|
| | 5 | | namespace Repository.Mapping; |
| | 6 | | public class DespesaMap : IEntityTypeConfiguration<Despesa> |
| | 7 | | { |
| | 8 | | public void Configure(EntityTypeBuilder<Despesa> builder) |
| 3 | 9 | | { |
| 3 | 10 | | builder.ToTable(nameof(Despesa)); |
| 3 | 11 | | builder.Property(d => d.Id).HasColumnType("varchar(36)") |
| 3 | 12 | | .HasConversion(v => v.ToString(), v => new Guid(v)) |
| 3 | 13 | | .IsRequired(); |
| 3 | 14 | | builder.HasKey(d => d.Id); |
| 3 | 15 | | builder.Property(d => d.Descricao).IsRequired(false).HasMaxLength(100); |
| 3 | 16 | | builder.Property(d => d.UsuarioId).HasColumnType("varchar(36)") |
| 3 | 17 | | .HasConversion(v => v.ToString(), v => new Guid(v)) |
| 3 | 18 | | .IsRequired(); |
| 3 | 19 | | builder.Property(d => d.CategoriaId).HasColumnType("varchar(36)") |
| 3 | 20 | | .HasConversion(v => v.ToString(), v => new Guid(v)) |
| 3 | 21 | | .IsRequired(); |
| | 22 | |
|
| | 23 | | // MySqlServer |
| 3 | 24 | | builder.Property(m => m.Data).HasColumnType("datetime").HasDefaultValueSql<DateTime>("CURRENT_TIMESTAMP").IsRequ |
| 3 | 25 | | builder.Property(m => m.DataVencimento).HasColumnType("datetime").HasDefaultValueSql(null); |
| | 26 | |
|
| | 27 | | // MsSqlServer |
| | 28 | | //builder.Property(d => d.Data).HasColumnType("datetime").HasDefaultValueSql<DateTime>("GetDate()").IsRequired() |
| | 29 | | //builder.Property(d => d.DataVencimento).HasColumnType("datetime").HasDefaultValueSql(null); |
| | 30 | |
|
| 3 | 31 | | builder.HasOne(d => d.Categoria) // Despesa -> Categoria |
| 3 | 32 | | .WithMany(c => c.Despesas) // Categoria -> Despesas |
| 3 | 33 | | .HasForeignKey(d => d.CategoriaId) |
| 3 | 34 | | .OnDelete(DeleteBehavior.Restrict); |
| | 35 | |
|
| 3 | 36 | | builder.Property(d => d.Valor).HasColumnType("decimal(10, 2)").HasDefaultValue(0); |
| 3 | 37 | | builder.HasOne(d => d.Usuario).WithMany().HasForeignKey(d => d.UsuarioId).OnDelete(DeleteBehavior.NoAction); |
| 3 | 38 | | } |
| | 39 | | } |