Many to Many Relationship

Posts   
 
    
Amith
User
Posts: 23
Joined: 21-Aug-2008
# Posted on: 25-Sep-2008 01:25:13   

How can I get rid of intermediate table in M:N relationship? Actually I don't want to map the middle table.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Sep-2008 07:09:35   

Hi Amith,

Could you please elaborate more on you scenario and what do you want to do? Do you want to hide the M:N relations? or just use them without map the intermediate entity? In order to build the relations and paths for an M:N relation, the intermediate entity is needed.

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 25-Sep-2008 10:19:09   

m:n relations are always using an intermediate table. This is because an m:n relation is a relation based on 2 m:1 relations.

Frans Bouma | Lead developer LLBLGen Pro
Amith
User
Posts: 23
Joined: 21-Aug-2008
# Posted on: 25-Sep-2008 17:34:12   

daelmo wrote:

Hi Amith,

Could you please elaborate more on you scenario and what do you want to do? Do you want to hide the M:N relations? or just use them without map the intermediate entity? In order to build the relations and paths for an M:N relation, the intermediate entity is needed.

What I mean is I don't want to map the middle table. Is there any way in the designer for that? For example, if I have Customer & Order and CustomerOrder table in the middle. I want to get all orders for customers with out going through CustomerOrderEntity. I mean like CustomerEntity.Relations.OrderEntityThroughCustomerFKId

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 26-Sep-2008 10:22:02   

I want to get all orders for customers with out going through CustomerOrderEntity. I mean like CustomerEntity.Relations.OrderEntityThroughCustomerFKId

If the relation is realized as m:n in the database, hoe could you get the above relation, if the OrderEntity won't have a CustomerId as a FK?

To get all Orders for a certain customer you have to pass through the middle table to link them together.

Amith
User
Posts: 23
Joined: 21-Aug-2008
# Posted on: 26-Sep-2008 22:22:37   

Walaa wrote:

I want to get all orders for customers with out going through CustomerOrderEntity. I mean like CustomerEntity.Relations.OrderEntityThroughCustomerFKId

If the relation is realized as m:n in the database, hoe could you get the above relation, if the OrderEntity won't have a CustomerId as a FK?

To get all Orders for a certain customer you have to pass through the middle table to link them together.

Ok may I asked in a wrong way. I know in database I have an intermediate table for m:n relationship. My question was is it possible after generating the entities can I access the second table with out going through the middle table? Like in Customer & Order, is it way I can get the orders with out going through the CustomerOrder entity? I mean like from Customer entity can I get the orders with out calling the CustomerOrder entity.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-Sep-2008 22:15:16   

LLBLGen Pro have full support for that. Let's see some examples:

In these examples I have three tables (_Product --(1:n)--> OrderDetail --(m:1)--> Order_) so there exists an M:N relation Products-->Order.

At LLBLGen Designer, for each M:N relation, a **FieldOnRelation **is created. In our example it resides on _Product _entity. We renamed from _OrderCollectionViaOrderDetails _to Orders, so at generated code we will have some _product.Order_s collection.

Let's retrieve all orders which contain the purchase of a product X with productID 10. We're not interested in the product entity itself so that's not fetched. We pass the Orders collection directly without storing it into another reference variable.

ProductEntity product = new ProductEntity(10);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(product.Orders, product.GetRelationInfoOrders());

Now an example with a product collection with some filter. Then we want to prefetch its related Orders (M:N).

EntityCollection<ProductEntity> products = new EntityCollection<ProductEntity>();
IRelationPredicateBucket filter = new RelationPredicateBucket(ProductFields.UnitPrice > 100);

// include the M:N path
IPrefetchPath2 path = new PrefetchPath2((int)EntityType.ProductEntity);
path.Add(ProductsEntity.PrefetchPathOrders);

// fetch the collection
adapter.FetchEntityCollection(products, filter, path);

// example iterating to access the M:N entities
foreach (ProductsEntity p in products)          
{
    Console.WriteLine(p.OrdersCollectionViaOrderDetails.Count);
    foreach (OrdersEntity od in p.OrdersCollectionViaOrderDetails)
    {
        Console.WriteLine(od.CustomerId);
    }
}
David Elizondo | LLBLGen Support Team
Amith
User
Posts: 23
Joined: 21-Aug-2008
# Posted on: 29-Sep-2008 20:04:07   

daelmo wrote:

LLBLGen Pro have full support for that. Let's see some examples:

In these examples I have three tables (_Product --(1:n)--> OrderDetail --(m:1)--> Order_) so there exists an M:N relation Products-->Order.

At LLBLGen Designer, for each M:N relation, a **FieldOnRelation **is created. In our example it resides on _Product _entity. We renamed from _OrderCollectionViaOrderDetails _to Orders, so at generated code we will have some _product.Order_s collection.

Let's retrieve all orders which contain the purchase of a product X with productID 10. We're not interested in the product entity itself so that's not fetched. We pass the Orders collection directly without storing it into another reference variable.

ProductEntity product = new ProductEntity(10);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(product.Orders, product.GetRelationInfoOrders());

Now an example with a product collection with some filter. Then we want to prefetch its related Orders (M:N).

EntityCollection<ProductEntity> products = new EntityCollection<ProductEntity>();
IRelationPredicateBucket filter = new RelationPredicateBucket(ProductFields.UnitPrice > 100);

// include the M:N path
IPrefetchPath2 path = new PrefetchPath2((int)EntityType.ProductEntity);
path.Add(ProductsEntity.PrefetchPathOrders);

// fetch the collection
adapter.FetchEntityCollection(products, filter, path);

// example iterating to access the M:N entities
foreach (ProductsEntity p in products)          
{
    Console.WriteLine(p.OrdersCollectionViaOrderDetails.Count);
    foreach (OrdersEntity od in p.OrdersCollectionViaOrderDetails)
    {
        Console.WriteLine(od.CustomerId);
    }
}

Thanks. Thats what I was looking for. I think I did some mistake. I re-run the designer after some changes in the DB & it's working fine