How do I define a database view using Entity Framework 4 Code-First?

前端 未结 2 500
臣服心动
臣服心动 2020-11-30 10:15

How do I define a database view using Entity Framework 4 Code-First? I can\'t find anything about this anywhere!

2条回答
  •  情歌与酒
    2020-11-30 10:33

    That's because you cannot define database view using code-first approach. Database view is database construct which uses SQL Query on top of existing tables / functions. You can't define such constructs using code first.

    If you want view you must create it manually by executing CREATE VIEW SQL script for example in custom initializer - it will be similar like this answer. Just be aware that this will not help you if you want to map entity to a view. In such case you would probably have to first drop table created by EF and create view with the same name (I didn't try it but it could do the trick). Also be aware that not every view is udpatable so you will most probably get read only entity.

提交回复
热议问题