准备三个Model

image.png
League

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Text;
  5. namespace Demo.Domain
  6. {
  7. public class League
  8. {
  9. public int Id { get; set; }
  10. [Required]
  11. [MaxLength(100)]
  12. public string Name { get; set; }
  13. [Required,MaxLength(100)]
  14. public string Country { get; set; }
  15. }
  16. }

Club

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4. namespace Demo.Domain
  5. {
  6. public class Club
  7. {
  8. public Club()
  9. {
  10. Players = new List<Player>();
  11. }
  12. public int Id { get; set; }
  13. public string Name { get; set; }
  14. public string City { get; set; }
  15. [Column(TypeName ="date")]
  16. public DateTime DateOfEstablishment { get; set; }
  17. public string History { get; set; }
  18. public League League { get; set; }
  19. public List<Player> Players { get; set; }
  20. }
  21. }

Player

  1. using System;
  2. using System.Collections.Generic;
  3. namespace Demo.Domain
  4. {
  5. public class Player
  6. {
  7. public Player()
  8. {
  9. GamePlayers = new List<GamePlayer>(); //避免空指针异常
  10. }
  11. public int Id { get; set; }
  12. public string Name { get; set; }
  13. public DateTime DateOfBirth { get; set; }
  14. public List<GamePlayer> GamePlayers { get; set; }
  15. //与Resume建立一对一的关系
  16. public int ResumeId { get; set; }
  17. public Resume Resume { get; set; }
  18. }
  19. }

源代码EFCoreDemo3.x.zip