开发者

Entity Framework 4, defining relationship

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)
      开发者_如何学Python      .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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜