2016-11-09 35 views
1

我有一个products表,其中CategoryId表示Categories表中的相应主键。根据外键ID将列值传递到不同的表格进行建模

ProductViewModel

public ProductVM(ProductDTO productDTO) 
{ 
    Id = productDTO.Id; 
    Name = productDTO.Name; 
    Description = productDTO.Description; 
    Price = productDTO.Price; 
    CategoryId = productDTO.CategoryId; 
    ImageName = productDTO.ImageName; 
} 

public int Id { get; set; } 
[Required] 
public string Name { get; set; } 
[Required] 
public string Description { get; set; } 
[Required] 
public decimal Price { get; set; } 

public int? CategoryId { get; set; } 
public IEnumerable<SelectListItem> Categories { get; set; } 
public string ImageName { get; set; } 
public IEnumerable<string> GalleryImages { get; set; } 

产品DTO

public class ProductDTO 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Slug { get; set; } 
    public string Description { get; set; } 
    public decimal Price { get; set; } 
    public int CategoryId { get; set; } 
    public string ImageName { get; set; } 

    [ForeignKey("CategoryId")] 
    public virtual CategoryDTO Category { get; set; } 
} 

这是我得到的产品列表:

List<ProductVM> productVM; 
using (Db db = new Db()) 
{ 
    productVM = db.Products 
     .ToArray() 
     .Select(x => new ProductVM(x)) 
     .ToList(); 
} 

正如你可以看到我传递了CategoryId左右,我可以用ProductVM ViewModel在我的视图中显示它,但是我也想要获得该类别中的Name

我可以想到一些黑客,例如,根据CategoryIdViewModel中的构造函数访问DB并指定它,但是我想查看是否有更优雅的解决方案?底线 - 我在我的Categories表中有一个Name列,我想以最有效的方式将该名称传递给ProductVM

+0

'productVM = db.Products.Include(X => X。类别)。选择(...'并为'CategoryName'添加一个属性,以便您可以使用'CategoryName = productDTO.Category.Name'; –

+0

@StephenMuecke我没有收到任何intellisense,'productVM = db.Products .include(x => x。)'给了我一个错误,我还添加了CategoryName proeprty并更新了我的Q. – frc

+0

什么错误? (并包括'使用System.Data.Entity;')? - 你总是可以使用'.Include(“Category”)'而不是 –

回答

1

添加属性,你建模的类别名称,说

public string CategoryName { get; set; } 

和修改构造函数来填充它

public ProductVM(ProductDTO productDTO) 
{ 
    CategoryName = productDTO.Category.Name; 
    .... 

,并修改查询到

List<ProductVM> productVM = db.Products.AsEnumerable().Include(x => x.Category) 
    .Select(x => new ProductVM(x)).ToList(); 

注您查看模型也需要一个无参数的构造函数,如果您使用它来编辑,否则一个e xception将在POST方法中抛出。

还要注意的是,你不需要using (Db db = new Db())

+0

你有没有运行这个?现在没有时间去测试它,但我不认为它可以工作。你不能在LinqToEntities上使用构造函数(当然你需要加载类) –

+0

@GeorgeVovos当然,作品(和OP已经在评论中证实了它)。当然你可以使用构造函数 –

+0

OP在选择...之前有一个ToArray()...所以这就是Linq到Objects –

0

最好的解决办法如下(我删除了所有其他的答案)

namespace Test 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.ComponentModel.DataAnnotations.Schema; 
    using System.Data.Entity; 
    using System.Linq; 

    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      List<ProductVM> productVM; 
      //It doesn't matter if you use a using block or not 
      using (Db db = new Db()) 
      { 
       db.Database.Log = Console.WriteLine;//To see the generated SQL 

       productVM = db.Products 
           .Include(p => p.Category) 
           .Select(p => new ProductVM 
           { 
            Id = p.Id, 
            Name = p.Name, 
            Description = p.Description, 
            Price = p.Price, 
            CategoryId = p.CategoryId, 
            CategoryName = p.Category.Name, 
            ImageName = p.ImageName, 
           }).ToList(); 



      } 

      Console.ReadKey(); 
     } 
    } 

    public class Db : DbContext 
    { 
     public DbSet<ProductDTO> Products { get; set; } 
     public DbSet<CategoryDTO> Categories { get; set; } 
    } 

    public class ProductDTO 
    { 
     [Key] 
     public int Id { get; set; } 

     public string Name { get; set; } 
     public string Slug { get; set; } 
     public string Description { get; set; } 
     public decimal Price { get; set; } 
     public int CategoryId { get; set; } 
     public string ImageName { get; set; } 

     [ForeignKey("CategoryId")] 
     public virtual CategoryDTO Category { get; set; } 
    } 

    public class CategoryDTO 
    { 
     [Key] 
     public int Id { get; set; } 

     public string Name { get; set; } 
    } 

    public class ProductVM 
    { 
     public int Id { get; set; } 

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

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

     [Required] 
     public decimal Price { get; set; } 

     public int? CategoryId { get; set; } 
     public string CategoryName { get; set; } 
     public string ImageName { get; set; } 
    } 
}