2

如何獲得一個類,使其具有由另一個模型組成的集合,並且在獲取我的原始模型時將其填充。我有一個願望清單,並且該願望清單中有零個或多個產品。如果我要執行db.Wishlist.find(id),那麼我的數據註釋或流利的API需要說些什麼才能填充。以下是我目前在我的收藏模型EF代碼首先一對多

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace Models 
{ 
    [Table("Wishlist")] 
    public class Wishlist 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     [ScaffoldColumn(false)] 
     public int ID { get; set; } 

     [StringLength(100)] 
     public string Name { get; set; } 

     public int ProductID { get; set; } 

     public virtual ICollection<Product> Product { get; set; } 

     public int CustomerID { get; set; } 

     [Required] 
     public Customer Customer { get; set; } 

     public virtual List<Product> Products { get; set; } 

     [DisplayFormat(DataFormatString = "{0:f}")] 
     public DateTime CreateDate { get; set; } 


     [DisplayFormat(DataFormatString = "{0:f}")] 
     public DateTime LastModifiedDate { get; set; } 


    } 
} 

需要什麼來讓產品來填充無論是作爲集合或列表。什麼是實現這一目標的正確途徑?我知道其中一個產品集合必須去,只是不知道哪些和需要什麼。

更新:增加了我的產品型號的顯示。

namespace Models 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.ComponentModel.DataAnnotations.Schema; 
    using System.Data.Entity.Spatial; 

    [Table("Product")] 
    public partial class Product 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public Product() 
     { 
      OrderLines = new HashSet<OrderLine>(); 
      SKU_Table = new HashSet<Sku>(); 
      XREF_CatalogProduct = new HashSet<XREF_CatalogProduct>(); 
      ProductImages = new List<ProductImage>(); 
     } 

     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int ID { get; set; } 

     [NotMapped] 
     public string FormattedPrice { get { return this.Price.ToString("C"); } } 

     [Required] 
     [MaxLength] 
     public string PageURL { get; set; } 

     [Required] 
     [StringLength(250)] 
     public string Name { get; set; } 

     [Required] 
     public string Code { get; set; } 

     public string Description { get; set; } 

     public int CategoryID { get; set; } 

     [Column(TypeName = "money")] 
     [DisplayFormat(DataFormatString = "${0:#,0}", ApplyFormatInEditMode = true)] 
     public decimal Price { get; set; } 

     public DateTime? DateCreated { get; set; } 

     public DateTime? DateModified { get; set; } 

     [Required]   
     public bool Featured { get; set; } 

     public virtual string ImagePath { get; set; } 

     public virtual Category Category { get; set; }   

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<OrderLine> OrderLines { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<Sku> SKU_Table { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<XREF_CatalogProduct> XREF_CatalogProduct { get; set; } 

     public virtual ICollection<ProductImage> ProductImages { get; set; } 
    } 
} 
+1

你能證明你的'Product'模式也檢索wishlist? – Sampath

+0

@Sampath更新了它 – ddeamaral

+0

你需要知道如何與'Wishlist'和'Product'建立'1:M'關係嗎? – Sampath

回答

2

你不得不安裝與Wishlist : Product。代碼一個M: M關係,如果你使用DataAnnotation首先會爲你創建一個Junction table

使用DataAnnotation:

[Table("Wishlist")] 
public class Wishlist 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [ScaffoldColumn(false)] 
    public int ID { get; set; } 

    [StringLength(100)] 
    public string Name { get; set; } 

    public int CustomerID { get; set; } 

    [Required] 
    public Customer Customer { get; set; } 

    [DisplayFormat(DataFormatString = "{0:f}")] 
    public DateTime CreateDate { get; set; } 


    [DisplayFormat(DataFormatString = "{0:f}")] 
    public DateTime LastModifiedDate { get; set; } 

    public virtual ICollection<Product> Products { get; set; } 

} 

而且

[Table("Product")] 
    public partial class Product 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public Product() 
     { 
      OrderLines = new HashSet<OrderLine>(); 
      SKU_Table = new HashSet<Sku>(); 
      XREF_CatalogProduct = new HashSet<XREF_CatalogProduct>(); 
      ProductImages = new List<ProductImage>(); 
      this.Wishlists = new HashSet<Wishlist>(); 

     } 

     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int ID { get; set; } 

     [NotMapped] 
     public string FormattedPrice { get { return this.Price.ToString("C"); } } 

     [Required] 
     [MaxLength] 
     public string PageURL { get; set; } 

     [Required] 
     [StringLength(250)] 
     public string Name { get; set; } 

     [Required] 
     public string Code { get; set; } 

     public string Description { get; set; } 

     public int CategoryID { get; set; } 

     [Column(TypeName = "money")] 
     [DisplayFormat(DataFormatString = "${0:#,0}", ApplyFormatInEditMode = true)] 
     public decimal Price { get; set; } 

     public DateTime? DateCreated { get; set; } 

     public DateTime? DateModified { get; set; } 

     [Required]   
     public bool Featured { get; set; } 

     public virtual string ImagePath { get; set; } 

     public virtual Category Category { get; set; }   

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<OrderLine> OrderLines { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<Sku> SKU_Table { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<XREF_CatalogProduct> XREF_CatalogProduct { get; set; } 

     public virtual ICollection<ProductImage> ProductImages { get; set; } 

     public virtual ICollection<Wishlist> Wishlists { get; set; } 


    } 

EF查詢:到根據product Id

var prod_id=1; // your product id 

var query= from wishlist in db.Wishlists 
      where wishlist.Products.Any(c=>c.Product_ID== prod_id) 
      select wishlist; 

使用流暢API檢索wishlist

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 

     modelBuilder.Entity<Wishlist>() 
        .HasMany<Product>(s => s.Products) 
        .WithMany(c => c.Wishlists) 
        .Map(cs => 
          { 
           cs.MapLeftKey("WishlistRefId"); 
           cs.MapRightKey("ProductRefId"); 
           cs.ToTable("WishlistProduct"); 
          }); 

    } 

EF查詢:到根據product Id

var prod_id=1; // your product id 

var query= from wishlist in db.Wishlists 
      where wishlist.Products.Any(c=>c.ProductRefId == prod_id) 
      select wishlist;