Parent Id not set in Child Object

Jan 5, 2011 at 7:58 PM

I have created a EF model that contains two tables, Foo and Bar.  Foo identity field is FooId.  Bar contains a foreign key named FooId to relate the Bar back to Foo.  This relationship is defined in SQL and the EF.  I have generated the CSLA 4.0 Busines Objects using the T4 template.  However, when I create my Foo object and try to reference the Foo.Bars, it is null.  So I added code that creates the Bar ChildList object as part of the AfterCreate method of Foo. 

   if (this.Bars == null) { this.Bars = DataPortal.CreateChild(this); }

Then I create a Bar BusinessBase object using:   

  var child = this.Bars.AddNew();

When I later call Foo.Save(), the the Bar.FooId field has not been populated that was populated when Foo was first created. Shouldn't the generated code handle setting the  FooId since it should know from the EF model what fields are related?  Am I expecting too much of the generated code?  If so, what partial method should I use to set the Bar.FooId? 

Coordinator
Jan 5, 2011 at 10:22 PM

Hi mosgatharp.

This issue is on my task list and will be resolved in future versions. Until then, you can use BeforeInsert partial method of Bar object to set FooID. This approach works also when Foo.ID value is a database generated (Identity).

Example:

partial void BeforeInsert()
{
	var list = (BarList)this.Parent;
	var foo = (Foo)list.Parent;
	this.FooID = foo.ID;
}

Regards,

Josip

Jan 7, 2011 at 1:58 PM

Thanks.  Being new to CSLA and the T4 template, I just wanted to make sure I wasn't missing something.

Feb 25, 2011 at 2:05 AM

Has this been addressed in an update yet??

 

Barryfz