Strange issue when adding entities to a EntityCollection

Posts   
 
    
pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 01-Apr-2007 00:17:17   

I have problems with this code:

    public static SerieEntity CreateSerieFromSerie(SerieEntity serie, InstrumentSemanticFields.SerieType toType)
    {
        SerieEntity ret = null;

        if (serie != null)
        {
            if (serie.TypeSm == (int)toType)
            {
                ret = serie;
            }
            else
            {
                ret = CreateSerie();
                ret.StartDate = GetFirstQuotationDate(serie);
                ret.Name = serie.Name;
                ret.TypeSm = (int)toType;

                int quotNum = serie.Quotation.Count;

                if (quotNum > 0)
                    ret.Quotation.Add(serie.Quotation[0]);

                if (quotNum > 1)
                {
                    int month = serie.Quotation[0].Date.Month;

                    for (int i = 1; i < quotNum; i++)
                    {
                        if (serie.Quotation[i].Date.Month > month)
                        {
                            month = serie.Quotation[i].Date.Month;
                            ret.Quotation.Add(serie.Quotation[i - 1]);
                        }
                    }
                }
            }
        }

        return ret;
    }

When i add entities in the EntityCollection named 'ret.Quotation', the entity is deleted from collection named 'serie.Quotation'.

Anyone can explain this?

Regards.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Apr-2007 02:51:59   

pvilanova, I suppose that's because the entity already belongs to other collection. I mean a quotation can't belong to two series, as an order can't belong to two customers, that's why LLBLGen remove the original reference PerformRelatedEntityRemoval).

However, I suggest create an entity clone. You can achieve that in this way simple_smile :

// create a new object to add
QuotationEntity cloneQuotation = new QuotationEntity();
cloneQuotation.Fields = ((EntityFields) serie.Fields).Clone();

// add the clone
cloneQuotation.IsNew = true;
ret.Quotation.Add(cloneQuotation);

Hope helpul wink

David Elizondo | LLBLGen Support Team
pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 01-Apr-2007 02:59:21   

Thanks for the reply.

Quotation collections use to be very big, and i don`t need to copy it, i only need a reference, and need to preserve the original collection. I think the decision of removing an entity from an entity collection is a developer decision, not a llblgen decision.

Regards.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Apr-2007 03:09:27   
Quotation collections use to be very big, and i don`t need to copy it, i only need a reference, and need to preserve the original collection. I think the decision of removing an entity from an entity collection is a developer decision, not a llblgen decision.

Well, as I see, its a consistency pro! that LLBLGen do that. However this is only the default behavior, so you can override the UnsetRelatedEntity method to avoid this or even modify the templates so that never happen for any entity. wink

David Elizondo | LLBLGen Support Team
pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 01-Apr-2007 03:15:10   

Please add a boolean property for the next llblgen version! wink

Regards.

pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 02-Apr-2007 23:51:16   

daelmo:

Please be more specific. In which class i must override UnsetRelatedEntity? And please give an example code.

Regards.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Apr-2007 10:28:01   

First of all let's examine the code below:


                        for (int i = 1; i < quotNum; i++)
                        {
                            if (serie.Quotation[i].Date.Month > month)
                            {
                                month = serie.Quotation[i].Date.Month;
                                ret.Quotation.Add(serie.Quotation[i - 1]);
                            }
                        }

For a Quotation entity in memory, it has one FK field pointing to a PK in a Serie Entity, and one corresponding property holding the related Serie Entity object.

Now for your situation, do you want the FK to hold values for both PKs of the "serie" instance and the "ret" instance? It can't be. Because when you assign an entity to be related to another entity FK-PK relations is set automatically.

Also quotation.Serie which is the related entity property itself, would hold reference to only one Serie entity. Because when you do: anySerie.Quotation.Add(anyQuotation) it automatically do: anyQuotation.Serie = anySerie

So maybe if you describe what you need that for, we can help you find a better way of doing it.

pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 03-Apr-2007 16:06:08   

walaa:

I've solved my problem.

Thanks a lot.