开发者

Many to Many relationship in EF

开发者 https://www.devze.com 2023-01-01 15:16 出处:网络
I have what seems to be a simple question about EF 1.0 and have been looking all over the web for a simple answer, but have had no luck so far:

I have what seems to be a simple question about EF 1.0 and have been looking all over the web for a simple answer, but have had no luck so far:

We're about to build a small application with SQL Server as the backend and have been considering using EF 1.0. Our company, like many large corpor开发者_运维技巧ations, does not move quickly in adopting new platforms, which means .NET 4.0 won't be installed on machines for quite a while, so EF 4.0 is not a possibility.

Here's the question (based on an extremely simple example):

Three tables in a many to many relationship:

Order: OrderID (PK), Customer, OrderDate

OrderDetails: OrderDetailID (PK), OrderID (FK), ProductID (FK), Quantity, BackOrdered

Product: ProductID (PK), ProductName, Category

What we what to create is an Order instance that has the Order information and a list of the OrderDetails including the ProductName

The SQL for this is trivial, so I won't bore you with that. And getting the Order and the list of OrderDetails is easy in EF 1.0, too. But when you get the list ProductID will identify the Product, and we need ProductName.

We've looked at this article. Is this the only way to accomplish what we need? Seems like a great deal of work...

Thanks for any help


Extrapolating from your description, and if a select is what you're looking for, something along those lines could work :

var query = from o in context.Order
                             .Include("OrderDetails")
                             .Include("OrderDetails.Product")
            select o;

you can then access the information through navigation properties, such as:

// using first here just for exemple purpose.
Order o = query.First();
var details = o.OrderDetails;
var aProductName = o.OrderDetails.First().ProductName;
0

精彩评论

暂无评论...
验证码 换一张
取 消