Home > Enterprise >  How to add one-to-many relationship data to the database in ASP.NET Core?
How to add one-to-many relationship data to the database in ASP.NET Core?

Time:08-20

I have two tables Person and Round, a person can participate in many rounds and a round can only contain one person.

How can I treat this case and add rounds to the database in an ASP.NET Core Web API?

Should it be in the person controller or round controller? And how to do it using Entity Framework? And should I include a PersonId attribute in the Round Post Dto class

Round class:

public class Round
{
    [Key] public int RoundId { get; set; }
    [Required] public int RoundNumber { get; set; }
    [Required] public int HostScore { get; set; }

    public Person Person { get; set; }
}

Person class:

public class Person
{
    [Key] public int PersonId { get; set; }
    [Required] public string Username { get; set; } = default!;
    [Required] public string Email { get; set; } = default!;
    [Required] public string Password { get; set; } = default!;

    public List<Round> Rounds { get; set; }
}

CodePudding user response:

You can do it like this:

public class Round
{
    [Key] public int RoundId { get; set; }
    [Required] public int RoundNumber { get; set; }
    [Required] public int HostScore { get; set; }

    [Required]
    [ForeignKey("Person")]
    public int PersonId { get; set; }
    public virtual Person Person { get; set; }
}

public class Person
{
    [Key] public int PersonId { get; set; }
    [Required] public string Username { get; set; } = default!;
    [Required] public string Email { get; set; } = default!;
    [Required] public string Password { get; set; } = default!;

    public virtual ICollection<Round> Rounds { get; set; }
}

If you don't have em you might need these usings also

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

CodePudding user response:

In ef, to identify the relationship you should use the Id word only in the key name of the parent class. for example:

public class Round
{
[Key] public int RoundId { get; set; }
[Required] public int RoundNumber { get; set; }
[Required] public int HostScore { get; set; }

public Person Person { get; set; }
public int PersonId { get; set; }
}


public class Person
{
[Key] public int Id { get; set; }
[Required] public string Username { get; set; } = default!;
[Required] public string Email { get; set; } = default!;
[Required] public string Password { get; set; } = default!;

public List<Round> Rounds { get; set; }
}

If you don't want to use this name, you can use fluent api

for example:

public class Round
{
    public int RoundId { get; set; }
    public int RoundNumber { get; set; }
    public int HostScore { get; set; }
    public Person Person { get; set; }
    public int PersonId { get; set; }

}


public class Person
{
    public int PersonId { get; set; }

    public string Username { get; set; } = default!;

    public string Email { get; set; } = default!;

    public string Password { get; set; } = default!;

    public List<Round> Rounds { get; set; }
}

public class RoundConfigurations : IEntityTypeConfiguration<Round>
{
    public void Configure(EntityTypeBuilder<Round> builder)
    {
        builder.HasOne(x => x.Person)
            .WithMany(x => x.Rounds)
            .HasForeignKey(x => x.PersonId);

        builder.HasKey(x => x.RoundId);

        builder.Property(x => x.HostScore).IsRequired();
        builder.Property(x => x.RoundNumber).IsRequired();

    }
}


public class PersonConfigurations : IEntityTypeConfiguration<Person>
{
    public void Configure(EntityTypeBuilder<Person> builder)
    {
        builder.HasKey(x => x.PersonId);

        builder.Property(x => x.Username).IsRequired();
        builder.Property(x => x.Email).IsRequired();
        builder.Property(x => x.Password).IsRequired();

    }
}
  • Related