DEV Community

Cover image for Advance Ef Core and PostgreSQL in .Net 6
Mohamad Lawand
Mohamad Lawand

Posted on

Advance Ef Core and PostgreSQL in .Net 6

In this article, we will be exploring advance Ef Core an PostgreSQL with .NET 6. The steps we need need to take to setup, configure and utilise PostgreSQL with .NET 6 WebAPI.

You can watch the full video on YouTube

You can also find the source code on Github

https://github.com/mohamadlawand087/Net6-EfCore-Postgres-V2

Changing Table Name in the database

modelBuilder.Entity<Driver>(entity =>
{
    entity.HasOne(d => d.Team)
        .WithMany(p => p.Drivers)
        .HasForeignKey(d => d.TeamId)
        .OnDelete(DeleteBehavior.Restrict)
        .HasConstraintName("FK_Driver_Team");

    entity.ToTable("F1Drivers");
});

modelBuilder.Entity<Team>().ToTable("F1Teams");
Enter fullscreen mode Exit fullscreen mode

Table Comments

modelBuilder.Entity<Team>()
        .ToTable("F1Teams")
        .HasComment("This table is used to store F1 teams information");
Enter fullscreen mode Exit fullscreen mode

Column Comments

modelBuilder.Entity<Team>()
        .ToTable("F1Teams")
        .HasComment("This table is used to store F1 teams information")
        .Property(b => b.Name)
        .HasComment("The Name of the team");
Enter fullscreen mode Exit fullscreen mode

Let us see how we can ignore properties from being migrated to the database

public class Driver : BaseEntity
{
    public Guid TeamId { get; set; }
    public string Name { get; set; } = "";
    public int RacingNumber { get; set; } 
    public virtual Team Team { get; set; }

    [NotMapped]
    public string NumberAndName { get; set; }
}

public class Team : BaseEntity
{
    public Team()
    {
        Drivers = new HashSet<Driver>();
    }

    public string Name { get; set; } = "";
    public string Year { get; set; } = "";

    [NotMapped]
    public string NameAndYear { get; set; }

    public virtual ICollection<Driver> Drivers { get; set; }
}

// Or with Fluent API
modelBuilder.Entity<Driver>(entity =>
{
    entity.HasOne(d => d.Team)
        .WithMany(p => p.Drivers)
        .HasForeignKey(d => d.TeamId)
        .OnDelete(DeleteBehavior.Restrict)
        .HasConstraintName("FK_Driver_Team");

    entity.ToTable("F1Drivers");
    entity.Ignore(x => x.NumberAndName);
});

modelBuilder.Entity<Team>()
        .ToTable("F1Teams")
        .HasComment("This table is used to store F1 teams information")
        .Ignore(x => x.NameAndYear);
Enter fullscreen mode Exit fullscreen mode

Update Column Name

// Fluent API
modelBuilder.Entity<Team>()
            .Property(b => b.Id)
        .HasColumnName("team_id");

public class Driver : BaseEntity
{
    public Guid TeamId { get; set; }
    public string Name { get; set; } = "";

    [Column("racing_number")]
    public int RacingNumber { get; set; } 
    public virtual Team Team { get; set; }

    [NotMapped]
    public string NumberAndName { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Column Data type

// Data Annotation
public class Driver : BaseEntity
{
    public Guid TeamId { get; set; }

    [Column(TypeName = "varchar(200)")]
    public string Name { get; set; } = "";

    [Column("racing_number")]
    public int RacingNumber { get; set; } 
    public virtual Team Team { get; set; }

    [NotMapped]
    public string NumberAndName { get; set; }
}

// Fluent API
modelBuilder.Entity<Driver>(entity =>
        {
            entity.HasOne(d => d.Team)
                .WithMany(p => p.Drivers)
                .HasForeignKey(d => d.TeamId)
                .OnDelete(DeleteBehavior.Restrict)
                .HasConstraintName("FK_Driver_Team");

            entity.Property(b => b.Name).HasColumnType("varchar(200)");

            entity.ToTable("F1Drivers");
            entity.Ignore(x => x.NumberAndName);
        });
Enter fullscreen mode Exit fullscreen mode

Column Order

modelBuilder.Entity<Driver>(entity =>
        {
            entity.HasOne(d => d.Team)
                .WithMany(p => p.Drivers)
                .HasForeignKey(d => d.TeamId)
                .OnDelete(DeleteBehavior.Restrict)
                .HasConstraintName("FK_Driver_Team");

            entity.Property(b => b.Name).HasColumnType("varchar(200)");

            entity.ToTable("F1Drivers");
            entity.Ignore(x => x.NumberAndName);

             entity.Property(b => b.Id)
            .HasColumnOrder(0);

            entity.Property(b => b.Name)
            .HasColumnOrder(1);

            entity.Property(b => b.RacingNumber)
            .HasColumnOrder(2);
        });
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
vivien7512 profile image
vivien7512

it looks good butè github source link is landing to a 404 error

Collapse
 
manoj_kumarsingh profile image
Manoj kumar Singh

still not resolved, 404 coming