Problems with ChildCollection

Nov 9, 2009 at 1:33 PM

Hy,

Today I found this software. I started be reading the Retina QuickStart manual to understand the principles of this OR/M.

First example, about creating a table into Retina database was easy, but in the second example, with 2 tables tbOrders and tbOrderLines I had some problems.

I create the two entities 

TypeStorage(TableName = "tbOrderLines")]public class OrderLine : Entity ....

and

<font size="2">

[

</font>

TypeStorage(TableName = "tbOrders")]

public class Order : Entity

{<font size="2">

[

</font>

FieldStorage(IsKey = true, IsIdentity = true, IdGeneratorType = typeof(IdGeneratorGuid))]<font size="2">

 

</font>

public Guid Id;

<font size="2">

[

</font>

FieldStorage]<font size="2">

 

</font>

public String Description = String.Empty;

 

 

 

<font size="2">

[

</font>

ChildStorage(ChildType = typeof(OrderLine), ChildPosition = "OrderLineIndex")]<font size="2">

[

</font>

ForeignKey(ParentFieldName = "Id", ChildFieldName = "OrderId ")]<font size="2">

 

</font>

 

public ChildCollection<OrderLine> OrderLines ;

}

In the test class I put the code from the Word document

 

 

 

 

 

<font size="2">

 

</font>

 

Order order = new Order();<font size="2">

order.Description =

</font>

"A special treat";<font size="2">

 

</font>

 

for (int i = 0; i < 10; i++){<font size="2">

 

</font>

 

OrderLine orderLine = new OrderLine();<font size="2">

orderLine.Description =

</font>

"OrderLine " + i;

orderLine.Price = i + 1.99;

orderLine.Quantity = 11 - i;

orderLine.OrderLineIndex = i;

orderLine.OrderId = order.Id;

order.OrderLines.Add(orderLine);

}

<font size="2">

order = (

</font>

Order)broker.Persist(order);

<font size="2">

When I try to execute this code the first exeption point to the  line of code 

 

</font>

[FieldStorage] public String Description = String.Empty;

from Order class and say that "The foreign key specified (OrderId ) does not exist on the type WindowsFormsApplication1.OrderLine"

The second exeption is when the OrderLine entity is created and it must be added in the ChildCollection collection of Order class.

Here its say the "Object reference not set to an instance of an object." because the collection OrderLines is not initialize. The question is HOW TO INITIALIZE the OrderLines collection because in the class ChildCollection DON'T HAVE A CONSTRUCTOR.

10x,

And I wait the response.

 

Daniel.

 

Coordinator
Nov 9, 2009 at 7:43 PM

Hi, thanks for trying Retina.NET.

The child entity NEEDS a foreign key field to its parent. Please make sure the OrderLines entity has an OrderId field with the right data type.

If the child collections are declared the way they are supposed, just creating a new entity will create the instace for every child collection. This is done internally by Retina.NET, because the base constructor initialize all this stuff. If you have added a constructor to your entity, please make sure you call the base one.

Best regards,

Andres.

Nov 9, 2009 at 8:48 PM

Hi,

Tanks for the tip. I looked again on the foreign key field and I saw a thing. I put a space by mistake on the ChildFieldName and that is the reason for all the exceptions that I found.

ForeignKey(ParentFieldName = "Id", ChildFieldName = "OrderId ")]<font size="2">

After I maked the modification the example worked very well. Tomorow I will read more and if I will have any other questions I will write to you.

10x for the patience.

Daniel.