2012-04-06 134 views
0

我試圖使用兩個INNER JOIN使用MVC3 LINQ多個INNER JOIN

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

return View(product.ToList()); 

如果我寫了上面的編碼,它的主要錯誤在返回查看(product.ToList());

錯誤表示'元數據集合中的多個項目與'模型'的標識匹配。

當我試着使用

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} 

我要添加INNER JOIN通過modelId到模型實體的外鍵。

怎麼可能?

謝謝。

--Added型號 -

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

您可以顯示域模型嗎? – ADIMO 2012-04-06 16:13:47

+0

使用'Include'時,包含的順序發生變化時通常會得到不同的結果。你嘗試過嗎?而當你添加兩個只包含其中的一個(甚至爲零)時,將會是內連接。 – 2012-04-08 21:10:21

回答

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] 

在我環境,包含接收lambda表達式沒有重載,你可能從某個地方得到它,所以我不知道發生了什麼您的包括,請嘗試更改您的查詢到

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

這是System.Data.Entity命名空間中的擴展方法。 – 2012-04-08 21:10:58