views:

96

answers:

1

When defining a relationship in entity framework 4 in a 1-to-many relationship using POCO classes why does the relationship have to be defined at the child level. for example say we have an order that has many products. The relationship in the mapping file for the product would look like:-

    Relationship(e => e.Order)
            .FromProperty(m => m.Product)
            .HasConstraint((e, m) => e.Id == m.Id);

In n-hibernate its defined in the mapping file at the parent level (Order in this case). having the relationship defined at the parent offers more flexibility and reuse.

Is there a way to do it at the parent level instead in EF4.

+1  A: 

In the EF4 CTP2 they do have inverse properties. They are mentioned in this ADO.NET team blog post.

 public ParentConfiguration()
        {
            Property(p => p.Id).IsIdentity();
            Property(p => p.FirstName).IsRequired();
            Property(p => p.LastName).IsRequired();

            //Register an inverse
            Relationship(p => p.Children).FromProperty(c => c.Parents);
        }

What this means is that parent.Children = children will work the same as child.Parents.Add(parent).

I have not seen a way to do it exactly like NHibernate where you can apply attribute\meta data directly to the parent class. In my experience working with POCO "plain old CLR objects" they are separate from the ORM framework. The relationships are defined by the ObjectContext in EF and are managed from there.

Jason Rowe
Your code will NOT setup an additional table with id mappings. What makes you think it will?
Alex James
I updated my response to try to make it more clear. Alex if I am way off please let me know.
Jason Rowe
Cool. Thanks Jason.
Alex James