0

I am using EfCore to build my Data access layer, the database is an sql server database.

After generating the Model using Scaffold-DbContext, I add some custom entities that I will fill up using custom sql query.

I wanted to know if there is someway to tell the DbContext that whenever I ask for CustomEntity use this query to fill it, without using FromSql("query"), so I can do this context.CustomEntity.ToList().

the Dbset is readonly, there is no need to track changes or anything else.

Houss_gc
  • 727
  • 5
  • 27
  • Make a database view and map to that. https://stackoverflow.com/questions/7461265/how-to-use-views-in-code-first-entity-framework – Steve Greene Aug 30 '17 at 20:11
  • there is no view in the db, that map to my custom entity. I have to get it from a custom SELECT. – Houss_gc Aug 30 '17 at 20:17
  • Well, if you can't add a view to the database could use an [interceptor](https://msdn.microsoft.com/en-us/library/dn469464(v=vs.113).aspx) in EF6. Looks like that is a work in progress for EF Core based on [this](https://github.com/aspnet/EntityFrameworkCore/issues/626) – Steve Greene Aug 30 '17 at 20:25
  • actually I can go for ef6 if efcore does not support what am I doing or maybe I will add some methods to mydbcontext in order to give A list full of customentites and hide the usage of FromSql inside it. – Houss_gc Aug 30 '17 at 20:41
  • You can certainly add whatever methods you like to your DbContext and return List for non-entity T. First-class support for this is on the EF Core Roadmap https://github.com/aspnet/EntityFrameworkCore/issues/9290 – David Browne - Microsoft Aug 30 '17 at 21:12
  • @David Browne - Steve Greene : Thank you for your answers, I will stick for now to adding methods that return me the wanted Entities list until it will be supported in EfCore. – Houss_gc Aug 30 '17 at 21:27

0 Answers0