Automapper and creating DTO class from stored procedure

  • Thread starter Thread starter Thomas Lee3
  • Start date Start date
T

Thomas Lee3

Guest
Hi All,

I have converted an existing database using the entity data model wizard (EF Designer from database). I am using automapper and have created a couple DTO classes. My problem is how to I add a stored procedure to one of my DTO classes. I am thinking this might be easier than I am making this out to be. I have seen several examples through out the web demonstrating simple classes, but I am have a database packed full of stored procedures and I am at a dead end.

I have attached a very basic example and I am hoping somebody my know how to create a DTO class from a stored procedure. In the example I have attached, the stored procedure has an input of an integer (Nullable) and will return a list of subscribed users.

The code...

// Generated from existing DB
public partial class MyDatabaseEntities : DbContext
{
public MyDatabaseEntities()
: base("name=MyDatabaseEntities")
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}

// .
// .
// .
public virtual ObjectResult<string> GetSubscribers(Nullable<int> iD)
{
var iDParameter = iD.HasValue ?
new ObjectParameter("ID", iD) :
new ObjectParameter("ID", typeof(int));

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<string>("GetSubscribers", iDParameter);
}

}

// My target DTO class
class GetSubscribersDTO
{
// not sure exactly how to map stored procedure
private List<string> subList;
}

// My init
public class AutoMapperConfig
{
public static void RegisterMappings()
{
var config = new MapperConfiguration(cfg = > {
cfg.CreateMap<MyDatabaseEntities, GetSubscribersDTO>();
});

}
}





Could somebody show me how to get this working? Ideally I would like to have a class with a few stored procedures.

Thanks,

Tom

Continue reading...
 
Back
Top