List <>가있는 모델 .. Entity Framework에서 어떻게 작동합니까?

아마드

저는 ASP.NET을 처음 사용하고 다른 모델 목록이있는 모델에 대해 CRUD 페이지를 만드는 방법이 궁금합니다. 모델에 기본 데이터 유형 만있는 간단한 시나리오를위한 CRUD 페이지를 만드는 이 튜토리얼을 따랐습니다 . 따라서 생성 된 DbContext는 자습서에서 사용 된 하나의 모델에만 해당됩니다. 그러나 다음 시나리오와 같이 둘 사이에 일대 다 관계가있는 두 개의 모델이있는 경우 어떻게합니까?

public class Player
{
    public string name {get; set;}
    public int age {get; set;}
    public double salary {get; set;}
    public string gender {get; set;}
    public DateTime contractSignDate {get; set;}
}

public class Team
{
    public string teamName {get; set;}
    public string sportPlayed {get; set;}
    public List<Player> players {get; set;}
}

팀 모델에 대한 CRUD 페이지를 만들려면 내가 만든 DbContext가 해당 모델 만 참조합니다. 사실, VS2013의 스캐 폴딩 엔진은 playersList와 함께하지도 않고 단순히 무시합니다.

이것을 해결하는 방법?

이것은 팀 모델을 위해 만든 DbContext입니다.

public class TeamDBContext : DbContext
{
    public DbSet<Team> Teams { get; set; }
}
Renakre

먼저 Player 클래스를 다음과 같이 정의합니다.

public class Player
{
  //you need to define an identifier
  public int PlayerId {get;set;}

  public string Name {get; set;}
  public int Age {get; set;}
  public double Salary {get; set;}
  public string Gender {get; set;}
  public DateTime ContractSignDate {get; set;}

  //you need a foreignkey
  [ForeignKey("TeamId")]
  public Team Team {get;set;}
  public int TeamId {get;set;}
}

그리고 팀 클래스 :

public class Team
{
   //you need an identifier
   public int TeamId {get;set;}

   public string TeamName {get; set;}
   public string SportPlayed {get; set;}
   public List<Player> Players {get; set;}
}

그리고 dbcontext 파일 :

public class TeamDBContext : DbContext
{
    public DbSet<Team> Teams { get; set; }
    public DbSet<Player> Players { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Player>()
                    .HasRequired(p => p.Team)
                    .WithMany(t => t.Players)
                    .HasForeignKey(p => p.TeamId)
    }
}

그리고 다음은 새로운 팀과 선수가되는 방법입니다.

TeamDBContext db = new TeamDBContext();

//create a new player
Player p = new Player() {Name="make", Age=10, Salary=10, Gender="m" };
db.Players.Add(p);
db.SaveChanges();

//create a new team
Team t = new Team () {TeamName="barcelona", SportType="soccer"};
db.Teams.Add(t);
db.SaveChanges();

//add a player to a team
db.Teams.Include("Players").Players.Add(p);
db.SaveChanges();

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

devise는 여러 모델에서 어떻게 작동합니까?

Entity Framework의 Find 메서드는 어떻게 작동합니까?

이 예제에서 List 모나드는 어떻게 작동합니까?

laravel 모델 관계는 코어에서 어떻게 작동합니까?

djangos 필드 인스턴스가 모델에서 어떻게 작동합니까?

Magento 1.9.2의 EAV 모델에서 load ()가 어떻게 작동합니까?

이 맥락에서 모델 관계는 어떻게 작동합니까?

모델 관계는 블레이드에서 어떻게 작동합니까?

Entity Framework 엔터티로드는 어떻게 작동합니까?

Entity Framework Core는 foreach ()와 어떻게 작동합니까?

Entity Framework Core 2.0에서 모델 자체 내에서 OnModelCreating을 어떻게 사용합니까?

Entity Framework 도메인 모델에서 뷰 모델을 자동 생성하는 도구가 있습니까?

C # Entity-Framework : 모델 개체에서 .Find 및 .Include를 결합하려면 어떻게해야합니까?

Entity Framework에서 "하위 모델"을 가질 수 있습니까?

Apache Spark는 메모리에서 어떻게 작동합니까?

Go에서 모듈 코드는 어떻게 작동합니까?

ArrayBuffer는 메모리에서 어떻게 작동합니까?

Django에서이 동작을 어떻게 모델링 할 수 있습니까?

<list> 태그는 봄에 어떻게 작동합니까?

A-Frame에서 작동하는 3D 모델에서 알파가있는 텍스처를 얻으려면 어떻게해야합니까?

동일한 필드가 있는 모델에 데이터를 추가하려면 어떻게 합니까?

pytorch의 seq2seq 모델에서 일괄 처리가 어떻게 작동합니까?

Laravel 4 모델 관계는 어떻게 작동합니까?

데이터베이스 및 모델과 관련하여 laravel에서 Tinker는 어떻게 작동합니까?

Entity에 Enum 변수가있는 경우 Entity에서 DTO로 어떻게 이동합니까?

Rails 4에 루비가있는 모델에 모듈 메서드를 어떻게 포함합니까?

(모든) 루프에서 정수 증가는 어떻게 작동합니까?

OracleForm에서 'tmpdata'와 'Get_Parameter_List'가 어떻게 작동합니까?

ngModel 지시문에 모델 이름을 제공하지 않고 ngModelChange는 어떻게 작동합니까?