New Posts New Posts RSS Feed: How to add to navigation property? (newbe q)
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

How to add to navigation property? (newbe q)

 Post Reply Post Reply
Author
patrickdk View Drop Down
Newbie
Newbie


Joined: 07-Oct-2009
Posts: 2
Post Options Post Options   Quote patrickdk Quote  Post ReplyReply Direct Link To This Post Topic: How to add to navigation property? (newbe q)
    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
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
patrickdk View Drop Down
Newbie
Newbie


Joined: 07-Oct-2009
Posts: 2
Post Options Post Options   Quote patrickdk Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down