1

我正在尝试使用两个外键在实体中显示两个值。

我有三张桌子;表之一是产品表。

两个表是用于显示这些值“名称”和“模型名称”的类别和模型。

当我使用 LINQ 时,我在添加模型实体之前使用了这种编码。

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

如何在此处添加模型实体?

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 中放置内部连接(?)。

谢谢你。

4

2 回答 2

1

我认为您可能需要以下内容,因为 Include 返回 IQueryable:

var product = from a in db.Product.Include(a => a.Category).Include(a => a.Model)
                  select a;
于 2012-04-06T11:54:00.133 回答
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();
        }
    }

然后在您的“Views\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>

    }

让我知道我是否走在正确的轨道上。如果是这样,我应该可以为您提供更多帮助。

最好的问候,尼克

于 2012-04-06T13:10:46.427 回答