0

Is it possible to define an Entity that is not mapped to a table in database and to use a stored procedure to return the entries?

I found that I can use "Ignore" so the table in database is not created for an Entity, but how can I set a stored procedure to populate data for this entity?

Note: I am using code first.

Thanks.

1 Answers1

0

You could create a normal model class that's not referenced by your database context. The model class should contain the properties you'll be returning from your stored proc. Then use

context.Database.ExecuteSqlCommand("storedProc", params)
// OR
context.Database.SqlQuery<YourEntityType>("storedProc",params);
William Robinson
  • 307
  • 2
  • 13
  • I am able to get them on that way, but what I need is the DbSet to be mapped to that SP, since I am using a datasource control (C1DataSource from ComponentOne Studio) that can be bound DbSets defined in the DbContext only (at first glance). – Fernando Pardo Aug 07 '17 at 13:39
  • Okay so add it to the context then use Ignore. – William Robinson Aug 07 '17 at 14:17
  • I already did that. What I need is a way to say to EF to use the resultset of a stored procedure to get entity entries, since it is not a table, it is a resultset. I think there is no way. – Fernando Pardo Aug 07 '17 at 18:15