2012-04-06 50 views
1

我想通過使用兩個外鍵在實體中顯示兩個值。MVC3無法顯示其他表值使用外鍵

我有三張表;表中的一個是產品表。

兩個表格分別是Category和Model,用於顯示這些值'name'和'modelName'。

當我使用LINQ時,我在添加Model實體之前使用了此編碼。

var product = from a in db.Product.Include(a => a.Category) 
         select a; 

如何在此處添加Model實體?

var product = from a in db.Product.Include(a => a.Category, a => a.Model) 
         select a; 

是否可以寫?

這是我的模特。

--Prodruct.cs-- 

public class Product 
{ 
    [Key] public int productId { get; set; } 

    [Required(ErrorMessage = "Please select category")] 
    public int categoryId { get; set; } 

    [Required(ErrorMessage = "Please select model")] 
    public int modelId { get; set; } 

    [DisplayName("Model name")] 
    public String model { get; set; } 

    public virtual Category Category { get; set; } 
    public virtual Model Model { get; set; } 
} 

--Category.cs-- 
public class Category 
{ 
    [Key] public int categoryId { get; set; } 
    public String name { get; set; } 
} 

--Model.cs-- 
public class Model 
{ 
    [Key] public int modelId { get; set; } 
    public String name { get; set; } 
} 

--RentalDB.cs-- 
public class rentalDB : DbContext 
{ 
    public DbSet<Product> Product { get; set; } 
    public DbSet<Model> Model { get; set; } 
    public DbSet<Customer> Customer { get; set; } 
    public DbSet<Order> Order { get; set; } 
    public DbSet<Cart> Cart { get; set; } 
    public DbSet<Category> Category { get; set; } 
    public DbSet<OrderDetails> OrderDetails { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    } 
} 

請讓我知道如何將內連接(?)放在LINQ中。

謝謝。

回答

1

我想你可能需要以下的自包含返回的IQueryable:

var product = from a in db.Product.Include(a => a.Category).Include(a => a.Model) 
        select a; 
+0

爲wholee1只是一個點:「LINQ」生成左邊加入了包括不內。如果你想檢查是否存在關係,你將不得不使用一些「where」 – 2012-04-06 12:20:05

+0

謝謝你的回覆,但是當我把你的編碼,我在'返回視圖(product.ToList());錯誤';'它說'元數據集合中的多個項目與'模型'的標識相匹配。是否有可能在linq中使用兩個實體類別和模型? – wholee1 2012-04-06 13:21:42

0

這是你在你的ProductController.cs需要什麼...

public ViewResult index(int param_categoryId, int param_modelId) 
    { 
     List<Product> locvar_CollectionOfProduct 
      = getCollectionOfProduct(param_categoryId, param_modelId); 

     return View("index", locvar_CollectionOfProduct); 
    } 

    private List<Product> getCollectionOfProduct(int param_categoryId, int param_modelId) 
    { 
     return db.Product.Where(a => a.categoryId == param_categoryId && a.modelId == param_modelId).ToList(); 
    } 

    public void Product_Save(List<Product> param_CollectionOfProduct) 
    { 
     if (Model.IsValid) 
     { 
      foreach (Product i_Product in param_CollectionOfProduct) 
      { 
       Product locvar_Product = null; 

       if (i_Product.productId == null || i_Product.productId == 0) 
       { 
        locvar_Product = new Product(); 
       } 
       else 
       { 
        locvar_Product = new Product{productId = i_Product.productId}; 
        db.Product.Attach(locvar_Product) 
       } 

       locvar_Product.categoryId = i_Product.categoryId; 
       locvar_Product.modelId = i_Product.modelId; 

       if (i_Product.productId == null || i_Product.productId == 0) 
       { 
        db.Product.Add(locvar_Product); 
       } 
      } 

      db.SaveChanges(); 
     } 
    } 

,然後在「查看\ Product \ index.cshtml「視圖,你可以遍歷這些。我會把它們放在你的桌子上:

@using insert_entity_reference_here.Models; 
    @model List<Product> 

    @{ 
     List<Product> param_CollectionOfProduct = Model; 
    } 

    @using (Ajax.BeginForm("Product_Save", "Product", null, new AjaxOptions { HttpMethod = "POST" })) 
    { 

    <table style="width:100%"> 

     <tr> 
      <th> 
       Category Name 
      </th> 
      <th> 
       Model Name 
      </th> 
     </tr> 

     @if(Model.Count() > 0) 
     { 

      for(i_Product = 0 ; i_Product < Model.Count() ; i_Product++) 
      { 

       @Html.HiddenFor(modelItem => param_CollectionOfProduct[i_Product].productId) 

       <tr> 
        <td> 
         @Html.HiddenFor(modelItem => param_CollectionOfProduct[i_Product].Category.categoryId) 
         @Html.EditorFor(modelItem => param_CollectionOfProduct[i_Product].Category.Name, new { style="width:100%" }) 
         @Html.ValidationMessageFor(modelItem => param_CollectionOfProduct[i_Product].Category.Name) 
        </td> 
        <td> 
         @Html.HiddenFor(modelItem => param_CollectionOfProduct[i_Product].Model.modelId) 
         @Html.EditorFor(modelItem => param_CollectionOfProduct[i_Product].Model.Name, new { style="width:100%" }) 
         @Html.ValidationMessageFor(modelItem => param_CollectionOfProduct[i_Product].Model.Name) 
        </td> 
       </tr> 
      } 
     } 

    </table> 

    <input type="submit">Save</input> 

    } 

讓我知道,如果我在正確的軌道上。如果是這樣,我應該能夠幫助你更多。

最好的問候, 尼克