I have the following function:
public IEnumerable<Task> AddTasks(IEnumerable<Task> tasks)
{
foreach (Task task in tasks)
{
Db.Tasks.Add(task);
}
Db.SaveChanges();
return tasks;
}
I need to insert multiple objects within this function. However when returning tasks, the objects are the same as when i sent those, TaskID (identity) still has a value of 0.
I tried this one after savechanges but it crashed:
//Revive object
foreach (Task task in tasks)
{
Db.Entry(task).GetDatabaseValues();
}
How can I obtain the identity of those items?
Edit:
public partial class Task
{
public int TaskID { get; set; }
public int ProjectID { get; set; }
public bool IsActive { get; set; }
public System.DateTime CreatedDate { get; set; }
}
public void AddTasksUsingViewModels(IEnumerable<TaskVM> taskVms)
{
IEnumerable<Task> tasksToAdd = taskVms.Select
(
t =>
new Task
{
ProjectID = taskVm.ProjectID,
IsActive = true,
CreatedDate = DateTime.Now
}
);
IEnumerable<Task> entityList = AddTasks(tasksToAdd);
}