Print Page | Close Window

How to add to navigation property? (newbe q)

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1511
Printed Date: 25-Mar-2025 at 1:46pm


Topic: How to add to navigation property? (newbe q)
Posted By: patrickdk
Subject: How to add to navigation property? (newbe q)
Date Posted: 07-Oct-2009 at 6:35am

Hi,

I'm new to Devforce, and trying to build my first app. What I'm trying is:

A domain model with two entities, Order and OrderDetail.
Order has an OrderDetails navigation property
OrderDetails does NOT have a navigation property to Order (I don't want all of my entities point to everything else)
 
My code looks like this:

var em = new DomainModel.DomainModelEntityManager(true);

var d = em.CreateEntity<DomainModel.OrderDetail>();
// set detail properties (...)
d.EntityAspect.AddToManager();
 
var o = em.CreateEntity<DomainModel.Order>();
// set order properties (...)
o.OrderDetails.Add(d);
o.EntityAspect.AddToManager();
 
em.SaveAsync(savedcallbackhandler, null);
 
I'm getting a NullReferenceException on o.OrderDetails.Add(d)
 
What am I doing wrong?
 
TIA,
 
Patrick



Replies:
Posted By: GregD
Date Posted: 07-Oct-2009 at 10:46am
Add the Order to the EntityManager before attempting to add to its OrderDetails collection.

Incidentally, when I run your code the exception I get is an InvalidOperationException, with the (actually quite helpful) message:

    Entities cannot be added to a RelatedEntityList belonging to a detached entity.

Incidentally, it seems quite odd to have a one-way relationship Order->OrderDetails but no OrderDetail->Order.  How do you model it?


Posted By: patrickdk
Date Posted: 07-Oct-2009 at 12:06pm
Hi Greg,

Steps to reproduce:
1. Create a database containing the two tables + relationship
2. Create EF model (from db)
3. edit xml, throw out navigationproperty Order from CSDL section (from entitytype orderdetails)
4. create model with devforce object mapper
5. execute code

Whether I add to EntityManager before or after results in the same error.

I can send you a sample project if desired.


Posted By: GregD
Date Posted: 07-Oct-2009 at 2:05pm
Well, you could do that, I suppose, but I'm still puzzled as to the utility of eliminating the navigation property back to the parent.



Print Page | Close Window