0
我正在嘗試使用實體框架,我認爲它很基本,但是我對EF很陌生,所以我需要幫助。我試圖使用EF和LINQ to檢索兩個表是在許多一對多關係記錄,因爲我將與下面的查詢SQL:實體框架4:如何獲取多對多關係中的記錄?
SELECT p.ProductName, pf.ProductFeatureName, pf.ProductFeatureDescription, pf.ProductFeatureActive
FROM ProductFeature pf
JOIN ProductProductFeature ppf ON
pf.ProductFeatureID = ppf.ProductFeatureID
JOIN Product p ON
ppf.ProductID = p.ProductID
一個表是產品,另一種是產品功能及其定義以及關聯表的定義如下:
TABLE Product (
ProductID INT PRIMARY KEY IDENTITY(1,1),
ProductCategoryID INT, -- FK to ProductCategory
ProductName NVARCHAR(255),
ProductDescription NVARCHAR(MAX),
ProductImagePath NVARCHAR(1024),
PricePerMonth DECIMAL(7,2), -- ex 11111.11
ProductActive BIT NOT NULL DEFAULT(1)
)
TABLE ProductFeature (
ProductFeatureID INT PRIMARY KEY IDENTITY(1,1),
ProductFeatureName NVARCHAR(255),
ProductFeatureSummary NVARCHAR(255),
ProductFeatureDescription NVARCHAR(MAX),
ProductFeatureActive BIT NOT NULL DEFAULT(1)
)
-- ProductCategory to Product association table
TABLE ProductProductFeature (
ProductProductFeature INT PRIMARY KEY IDENTITY(1,1),
ProductID INT, -- FK to Product
ProductFeatureID INT -- FK to ProductFeature
)
這怎麼實現?
謝謝你,Adilson。我會試試這個。另外,出於好奇,是否可以使用lamba-extension方法類型語法編寫等效查詢? – campbelt
我認爲你可以用lambda語法來做所有事情,因爲linq只是IQueryable方法的「語法糖」。 –