簡體   English   中英

ASP.NET Core中如何將一對多的關系數據添加到數據庫中?

[英]How to add one-to-many relationship data to the database in ASP.NET Core?

我有兩張表PersonRound ,一個人可以參加很多輪,一輪只能包含一個人。

如何處理這種情況並在 ASP.NET 核心 Web API 中向數據庫添加回合?

它應該在人 controller 還是圓形 controller 中? 以及如何使用實體框架來做到這一點? 我是否應該在Round Post Dto class 中包含PersonId屬性

圓形 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; }
}

人 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; }
}

你可以這樣做:

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; }
}

如果您沒有 em,您可能還需要這些使用

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

在 ef 中,要識別關系,您應該只在父 class 的鍵名中使用 Id 詞。 例如:

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; }
}

如果不想使用這個名字,可以使用fluent api

例如:

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();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM