Entity Framework 4 Self Many-To-Many with Properties

Posted by csharpnoob on Stack Overflow See other posts from Stack Overflow or by csharpnoob
Published on 2010-06-08T21:51:11Z Indexed on 2010/06/08 22:52 UTC
Read the original article Hit count: 237

UPDATE:

Solved by myself. Tricky but works. If you know a better solution, feel free to correct me.

DESIGNER:

alt text

CODE:

Product product1 = new Product{key = "Product 1"};
sd.AddToProducts(product1);

Product product2 = new Product{key = "Product 2"};           
sd.AddToProducts(product2);

Product product3 = new Product{key = "Product 3"};
ProductRelated pr = new ProductRelated();
pr.Products.Add(product1);
pr.Products.Add(product2);
product3.ProductRelateds.Add(pr);
sd.AddToProducts(product3);

CODE VIEW:

foreach(var x in (from b in sd.Products select b))
{
        %><%=x.key %><br />
        <%
            foreach (var y in x.ProductRelateds)
            {
                foreach (var k in y.Products)
            {
                %>- <%=k.key%><br /><%}
            }
}

OUTPUT

Product1
Product2
Product3
- Product2
- Product1

QUESTION:

Hi,

i want to have a Self Reference for Releated Products on a Product Entity, something like here:

http://my.safaribooksonline.com/9781430227038/modeling_a_many-to-many_comma_self-refer

But i also want on the Many-To-Many Reference addional Properties like deleted, created etc.

I tried to do it by another Entity "Related", but somehow it won't work. Does anyone had this Problem before? Is there any other Example?

Thanks

© Stack Overflow or respective owner

Related posts about .NET

Related posts about entity-framework