2012-03-25 2 views
1

코드를 사용하여 EF를 배우려고하는데,이 방법을 사용하여 올바르게 디자인하는 법을 모릅니다. 나에게EF 코드 첫 번째 테이블 관계

내 수업을 도와주세요 것은 내가 외래 키가되도록 StockUnitOfMeasurePurchaseUnitOfMeasure을 갖고 싶어이

public class Item 
{ 
    public int ItemID{ get; set; } 

    public string Name { get; set; } 
    public int StockUnitOfMeasure{ get; set; } 
    public int PurchaseUnitOfMeasure{ get; set; } 
} 

public class UnitOfMeasure 
{ 
    public int UnitOfMeasureID { get; set; } 
    public string MeasureName { get; set; } 
} 

것 같습니다.

는 여기에서 그는 샘플 데이터 내가 Id으로 FK 속성을 접미사로 한 선명도와 컨벤션

ItemID  Name StockUnitOfMeasure PurchaseUnitOfMeasure 
1   Apples  2      1 
2   Milk  3      4 

UnitOfMeasureID  MeasureName 
1     Piece 
2     Dozen 
3     Box 
4     Packs 

meaning: 
apples are stocked at the warehouse by DOZEN, but will be purchased per PIECE 
Milks are stocked at the warehouse by BOX, but will be purchased per PACK 

답변

1

입니다. 필요에 따라 이름을 바꿀 수 있습니다.

빠른 답변
+0

많은 감사

public class Item { public int ItemID { get; set; } public string Name { get; set; } public int StockUnitOfMeasureId { get; set; } public UnitOfMeasure StockUnitOfMeasure { get; set; } public int PurchaseUnitOfMeasureId { get; set; } public UnitOfMeasure PurchaseUnitOfMeasure { get; set; } } class MyContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Item>().HasRequired(x => x.StockUnitOfMeasure) .WithMany() .HasForeignKey(x => x.StockUnitOfMeasureId).WillCascadeOnDelete(true); modelBuilder.Entity<Item>().HasRequired(x => x.PurchaseUnitOfMeasure) .WithMany() .HasForeignKey(x => x.PurchaseUnitOfMeasureId).WillCascadeOnDelete(true); base.OnModelCreating(modelBuilder); } } 
그러나 나는이 오류 {는 "참조 관계는 허용되지 않는 순환 참조가 발생합니다. [제약 조건 이름 = Item_PurchaseUnitOfMeasure]"} – samantha07

+0

@ samantha07은'.WillCascadeOnDelete를 설정 발생 (true)'를 .WillCascadeOnDelete (false)'로 설정하십시오. – Eranga