Search code examples
c#entity-frameworkef-code-firstdto

Do I need to use DTO or POCO


I have a windows service application. The clients are scattered in the different place in US. When the service is consumed, then the data is generated. Now I have to save the data populated by the clients to sql server 2008 r2.

For the database, I used Entity FrameWork code first to generate it. Now it is empty in tables.

public class MyContext : DbContext, IDisposable
{
    public DbSet<Task> Tasks { get; set; }
}

 public class Task
{
    public int TaskId { get; set; }
    public string Detail { get; set; }
}

I am not sure the best pattern to save the data to db. Do I have to adopt data transfer object in somewhere to do it or save data directly? Code example demo is welcomed.


Solution

  • Once you have some data,

    Task t = new Task { Detail = ...  };
    
    using (var context = new MyContext())
    {
        context.Tasks.Add(t);
        context.SaveChanges();
    }