1

2 つの外部キーを使用して、エンティティに 2 つの値を表示しようとしています。

3 つのテーブルがあります。テーブルの 1 つは製品テーブルです。

2 つのテーブルは、これらの値 '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に内部結合(?)を入れる方法を教えてください。

ありがとうございました。

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 に答える