2016-04-29 22 views

回答

1

如果您product POCO,你有Category導航屬性Product,簡單使用:

@Html.DisplayNameFor(modelItem => model.Category.Title) 

假設你的產品和類別如下:

public class Product { 
    public int ID { get; set; } 
    public string ProductName { get; set; } 
    public int CategoryId { get; set; } 

    // and other properties 

    public virtual ProductCategory ProductCategory { get; set; } 
} 

public class ProductCategory { 
    public int ID { get; set; } 
    public string CategoryName { get; set; } 
    public virtual ICollection<Product> Products { get; set; } 
} 

,並在地圖中定義product您有:

internal class ProductMap : EntityTypeConfiguration<Product> { 
    public ProductMap() { 
     // Primary Key 
     this.HasKey(t => t.ID); 

     // Properties, for example 
     this.Property(t => t.ProductName) 
      .HasMaxLength(200); 
     // Table Mappings 
     this.ToTable("Product"); 

     //Relationships 
     this.HasRequired(t => t.ProductCategory) 
      .WithMany(t => t.Products) 
      .HasForeignKey(d => d.CategoryId); 

    } 
} 
相關問題