I am trying to return a list of object from my mapper and save them to my Entity Framework database. My mapperclass looks like this

    class TimereportMappers
    {        
    public List<Days> dayMap(List<Day> days)
    {
        List<Days> output = new List<Days>();

        foreach (Common.Day input in days)
        {
            output.Add(new Days()
            {
                Date = input.Date,
                Hour = input.Hours
            });
            //output.Date = input.Date;
            //output.Hour = input.Hours;
        }

        return output;
    }

}

The problem in my dataaccess-class is that i dont know how to save the objects to my database, the method in dataaccess-class looks like this

     public void sendDays(List<Common.Day> days)
        {
            TimereportMappers mapper = new TimereportMappers();
            context.Days.AddObject(mapper.dayMap(days));         
            context.SaveChanges();
        }

But AddObject can only add 1 object? How do i add a list of objects?

Visual Studio gives me the errorcodes

Error 2 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'Timereport.BusinessLogic.Data_Access.Days' C:\Users\widde\Documents\Visual Studio 2010\Projects\Timereport\Timereport.BusinessLogic\TimereportDataAccess.cs 43 37 Timereport.BusinessLogic

and

Error 1 The best overloaded method match for 'System.Data.Objects.ObjectSet.AddObject(Timereport.BusinessLogic.Data_Access.Days)' has some invalid arguments C:\Users\widde\Documents\Visual Studio 2010\Projects\Timereport\Timereport.BusinessLogic\TimereportDataAccess.cs 43 14 Timereport.BusinessLogic

有帮助吗?

解决方案

Would this work? (I can't obvously test this but the idea is to only add 1 object instance at a time instead of the whole list)

sendDays Method:

public void sendDays(List<Common.Day> days)
{
    TimereportMappers mapper = new TimereportMappers();

    foreach (var day in days)
    {
        context.Days.AddObject(mapper.dayMap(day));
    }

    context.SaveChanges();
}

TimereportMappers class:

class TimereportMappers
{
    public Days dayMap(Day input)
    {
        return new Days
        {
            Date = input.Date,
            Hour = input.Hours
        };
    }

}

其他提示

call AddObject() mutil-times and call SaveChanges() once?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top