Entity Collection generation

Posts   
 
    
rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 04-May-2005 19:01:04   

Hi second question of the day, sorry guys! but you are efficient.

Is there a quick way to generate an Entity from a dataset without me generating the code to say that this property = this property etc etc.

I have the following: I am running a stored procedure which returns the full table and obviously each row maps to the entity. I cant seem to find a genration routine that will take a row/dataset and create/fill the Entity for that object... Does this exist?

Again many thanks!

Rich

Answer
User
Posts: 363
Joined: 28-Jun-2004
# Posted on: 04-May-2005 19:32:57   

No it does not. However, i too would find something like this useful.

rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 04-May-2005 19:56:39   

OK cool... at least I wasn;t missing anything.. cheers for the shout

Rich

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 04-May-2005 21:47:05   

The loop is very small.


CustomerCollection col = new CustomerCollection();

// generic routine
foreach(DataRow row in datatable.Rows)
{
    IEntity e = col.EntityFactoryToUse.Create();
    for(int i=0;i<datatable.Columns.Count;i++)
    {
        e.SetNewFieldValue(i, row[i].Value);
    }
    col.Add(e);
}

(from my bare head, but it should work. You can also index on column name if you want that)

Frans Bouma | Lead developer LLBLGen Pro
rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 05-May-2005 16:36:59   

Hi many thanks for your code...

just a quick question on the code snippet below:

I get an exception generated that indicates that the The field 'CategoryId' is read-only and can't be changed thrown on the call to SetNewFieldValue.

Is there an option when you generate your entity classes to produce Get AND Set methods so that this exception wont be thrown?

See below for code snippet:

Cheers

Rich

SupportCategoryCollection collection = new SupportCategoryCollection();

        DataTable tb = RetrievalProcedures.RumGetSupportCategories();


        // generic routine
        foreach(DataRow row in tb.Rows) {
            SupportCategoryEntity e = new SupportCategoryEntity();
            for(int i=0;i<tb.Columns.Count;i++) {
                e.SetNewFieldValue(i, row[i]);
            }
            collection.Add(e);
        }

        return collection;
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 05-May-2005 17:27:09   

You can check in the loop to see if the field is a read only field. If it is readonly, its probably a pk field and you can exclude it.

I think.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-May-2005 10:26:46   

Devildog74 wrote:

You can check in the loop to see if the field is a read only field. If it is readonly, its probably a pk field and you can exclude it.

I think.

You can indeed check it in the loop. In selfservicing (which you use), Identity fields can't be set to a value using this mechanism, you have to use entity.Fields[index].ForcedCurrentValueWrite(value);

Frans Bouma | Lead developer LLBLGen Pro