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)
            .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 attributemeta 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.

链接地址: http://www.djcxy.com/p/60266.html

上一篇: 具有现有域模型的实体框架4

下一篇: 实体框架4,定义关系