Save List<DTO>

Posts   
 
    
samvarg
User
Posts: 25
Joined: 26-Feb-2010
# Posted on: 09-Jul-2010 19:48:45   

I am using the LLBLGEN generated DTO classes. I am having a method to save multiple records to the database.

I am passing the List<DTO> to the method. How do I save this to the database. If it was a collection I should use adapter.SaveEntityCollection. Is there a way I could convert to List<DTO> to EntityCollection<Entity>

thanks Sam

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Jul-2010 20:51:11   

Hi Sam,

You can't save DTO's as they don't have any persistence information. You will have to iterate your list, create entities for each DTO and add them to a EntityCollection, then you can save the EntityCollection.

David Elizondo | LLBLGen Support Team
samvarg
User
Posts: 25
Joined: 26-Feb-2010
# Posted on: 09-Jul-2010 21:05:42   

Thanks for your quick reply. Is there any examples I can find in your website or Help.

Naveen
User
Posts: 20
Joined: 28-Jun-2010
# Posted on: 09-Jul-2010 21:15:32   

Pretty straight forward

    public void SaveData(List<YourEntityDTO> dtoList)
    {
        YourEntityCollection coll = new YourEntityCollection();
        dtoList.ForEach(dto => coll.Add(new YourEntity(dto)));
        coll.SaveMulti();
    }
samvarg
User
Posts: 25
Joined: 26-Feb-2010
# Posted on: 09-Jul-2010 21:39:44   

Looks pretty straight forward. But I am getting an error. below is example I did and I got best overloaded method have invalid arguments at line empList.Foreach.

public void SaveEmp(List<EmpDTO> empList) { bool success = false; EntityCollection<EmpEntity> empCollection = new EntityCollection<EmpEntity>(); empList.ForEach(dto => empCollection.Add(new EmpEntity(dto)));

            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                success = adapter.SaveEntityCollection(empCollection);
            }
        }

I think I am doing something wrong. Since I am new to .Net and LLBLGEN I am not able to figure this out.

Thanks for the help

Naveen
User
Posts: 20
Joined: 28-Jun-2010
# Posted on: 09-Jul-2010 21:42:25   

Which version of .NET framework are you using? Can you please send the stack trace?

samvarg
User
Posts: 25
Joined: 26-Feb-2010
# Posted on: 09-Jul-2010 21:46:03   

I am using .Net frame work 3.5. I am not able to compile this because of the error.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Jul-2010 03:37:56   

No Entity constructor receives a DTO, that's why your compilation error. The idea is this:

bool success = false;
EntityCollection<EmpEntity> empCollection = new EntityCollection<EmpEntity>();
foreach (var x in empList)
{
     EmpEntity e = new EmpEntity();
     e.EmpId = x.EmpId;
     e.AnotherField = x.Anotherfield;
     ...
     empCollection.Add(e);  
}

using (DataAccessAdapter adapter = new DataAccessAdapter())
{
     success = adapter.SaveEntityCollection(empCollection);             
}
David Elizondo | LLBLGen Support Team
samvarg
User
Posts: 25
Joined: 26-Feb-2010
# Posted on: 12-Jul-2010 15:11:55   

Thanks. that worked.