2

Is it possible to map an entity to a database view using code-first?

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
czetsuya
  • 4,773
  • 13
  • 53
  • 99
  • How is it code first then? Or do you mean using the dbcontext api? – Erik Funkenbusch Nov 02 '11 at 04:44
  • possible duplicate of [How map objects to a view with EF 4 code first?](http://stackoverflow.com/questions/3595786/how-map-objects-to-a-view-with-ef-4-code-first) – Merlyn Morgan-Graham Nov 02 '11 at 04:45
  • @MerlynMorgan-Graham, as you can see I often mark some answers as useful then tag it as answer if it's really my accepted answer, but on some cases it really is not. – czetsuya Nov 02 '11 at 05:23

2 Answers2

1

Yes you can. You have to construct the View manually however. This can be done using a custom database initializer. Then you can just map an entity to a view since the underlying SQL syntax is the same. See: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/f154595c-717e-4703-a81d-ee63633a481e

EDIT as Merlyn Morgan-Graham points out, this is a duplicate to Is it possible to add a view in code-first context?

Community
  • 1
  • 1
Polity
  • 14,734
  • 2
  • 40
  • 40
0

Instead of adding a view, an alternative is to call a stored procedure using the DbContext class.

_context.Database.SqlQuery<Model>("spTest").ToList();
czetsuya
  • 4,773
  • 13
  • 53
  • 99