2011-09-08 3 views
0
public class Purchase { 
    public Address to { get; set; } 
    public Address from { get; set; } 

} 
public class Address { 
    public string name { get; set; } 
} 

나는 2 개의 주소가있는 1 개의 구매가 있습니다. 이것이 Entity Framework에서 사용할 외래 키를 포함하여 데이터베이스 (mySql)에 있어야하는 방법.2 같은 유형의 객체가 MySQL DB에 매핑 문제

내가 주소에서 탐색 용이성인지 (FK 기준) 엔티티가 이해하는 문제가 많은 1 (*) 내가 구매에 주소 목록이없는, 내가 정의 2.

감사합니다 , 바트.

답변

1

당신은이 같은 모델,

public class PurchaseConfiguration : EntityTypeConfiguration<Purchase > 
{ 
     public PurchaseConfiguration() 
     { 

      HasRequired(p=>p.to).WithOptionalDependent().WillCascadeOnDelete(false); 
      HasRequired(p => p.from).WithOptionalDependent().WillCascadeOnDelete(false); 
     } 

} 

하고 DB 컨텍스트는이 같은 구성을 추가 할 수 tables.Build의 관계를 구성 할 수

public class yourDbContext:DbContext 
{ 
     public DbSet<Purchase> Purchases{ get; set; } 
     public DbSet<Address> Addresses{ get; set; } 
     //other db sets here.. 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Configurations.Add(new PurchaseConfiguration()); 
      // you can add configurations for other tables 
     } 
} 
+0

감사합니다, unfortunatelly, 난 엔티티에 새로 도입되었습니다. 내가하는 일이 무엇인지, 어떻게하고, 어디에서 사용해야하는지 잘 이해하면 좋을 것입니다. 좀 더 설명해 주시겠습니까? –

+0

답변을 수정했습니다. 나는 당신이 처음에 이것과 같은 튜토리얼을 볼 필요가 있다고 생각합니다 .http : //www.asp.net/entity-framework/tutorials/creating-a-more-complex-data-model-for-an-asp-net- mvc-application –