2016-09-19 3 views
2

저는 ASP.NET Core와 EF Core를 사용하고 있으며, 다음 두 개의 부모 클래스와 하위 클래스가 있습니다. 각 선물 카드는 많은 트랜잭션을 가질 수데이터 주석이있는 EF 핵심 마이그레이션에서 외래 키를 NOT NULL로 만드는 방법은 무엇입니까?

내가 읽은 것을 바탕으로
public class GiftCard 
{ 
    public int Id { get; set; } 
    public string BarCode { get; set; } 
    public DateTime PurchaseDate { get; set; } 
    public string Comments { get; set; } 
    public byte[] Timestamp { get; set; } 
    public List<Transaction.Transaction> Transactions { get; set; } 
} 

public class Transaction 
{ 
    public int Id { get; set; } 
    public DateTime TransactionDate { get; set; } 
    public decimal TransactionAmount { get; set; } 
    public TransactionType TransactionType { get; set; } 
    public byte[] Timestamp { get; set; } 
    public GiftCard.GiftCard GiftCard { get; set; } 
}  

,이 아이의 부모와 참조 탐색에 대한 탐색을 갖는함으로써, 할 수있는 방법입니다. 마이그레이션을 추가하고 명령 줄을 사용하여 데이터베이스를 업데이트하면 Transactions 테이블의 GiftCardId 외래 키가 Null 인 것을 제외하고는 데이터베이스에서 모든 것이 정상적으로 보였습니다. 이 NULL이 아닌지 확인하고 싶습니다. Data Annotation 속성이 누락 되었습니까?

답변

2

Transaction 엔티티에 다음 특성을 입력하면 해결되어야합니다. 당신의 정의에 무슨 일이 일어나고 무엇

public int GiftCardId { get; set; }

는 그림자 속성이 생성되고 있으며 EF의 변경 추적기 관계를 유지하고 있다는 점이다.

here을 참조하십시오.