0

を使用して2つのINNER JOINを使用しようとしています

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

return View(product.ToList());

上記のコーディングを書くと、エラーが返されます View(product.ToList());

エラーは、「メタデータ コレクション内の複数のアイテムが ID 'モデル' と一致します。」

でデバッグしようとすると

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

SQLクエリを次のように見ることができます

{SELECT 
[Extent1].[productId] AS [productId], 
[Extent1].[categoryId] AS [categoryId], 
[Extent1].[modelId] AS [modelId], 
[Extent1].[model] AS [model], 
[Extent1].[displaySize] AS [displaySize], 
[Extent1].[processor] AS [processor], 
[Extent1].[ramSize] AS [ramSize], 
[Extent1].[capacityType] AS [capacityType], 
[Extent1].[capacity] AS [capacity], 
[Extent1].[colour] AS [colour], 
[Extent1].[description] AS [description], 
[Extent1].[price] AS [price], 
[Extent1].[threeDayPrice] AS [threeDayPrice], 
[Extent1].[aWeekPrice] AS [aWeekPrice], 
[Extent1].[twoWeekPrice] AS [twoWeekPrice], 
[Extent1].[aMonthPrice] AS [aMonthPrice], 
[Extent1].[threeMonthPrice] AS [threeMonthPrice], 
[Extent1].[sixMonthPrice] AS [sixMonthPrice], 
[Extent1].[stock] AS [stock], 
[Extent2].[categoryId] AS [categoryId1], 
[Extent2].[name] AS [name]
FROM  [dbo].[Product] AS [Extent1]
INNER JOIN [dbo].[Category] AS [Extent2] ON [Extent1].[categoryId] = [Extent2].[categoryId]
ORDER BY [Extent1].[model] ASC}

modelId を外部キーとして INNER JOIN を Model エンティティに追加したいと考えています。

それはどのように可能ですか?

ありがとう。

--追加モデル--

--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>();
}
}
4

1 に答える 1

0

私のテストデータベースでは、これをLinqPadで実行しました

from a in Products.Include("Categories").Include("Suppliers") select a

そして、私はこの生成されたSQLを取得しました:

SELECT 
[Extent1].[ProductID] AS [ProductID], 
[Extent1].[ProductName] AS [ProductName], 
... Lots of stuff ...
FROM   [dbo].[Products] AS [Extent1]
LEFT OUTER JOIN [dbo].[Categories] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]
LEFT OUTER JOIN [dbo].[Suppliers] AS [Extent3] ON [Extent1].[SupplierID] = [Extent3].[SupplierID]

私の環境では、インクルードがラムダ式を受け取るためのオーバーロードはありません。おそらくどこかから取得したので、インクルードで何が起こっているのかわかりません。クエリを次のように変更してみてください

var product = from a in db.Product.Include("Category").Include("Model") select a;
于 2012-04-06T16:20:07.723 に答える