none
Views in Code First data service RRS feed

  • Question

  • Hi

    I have implemented some business logic in sql views(For the sample DB first approach we can add views us an entity) , same in code first how can we use views? is it possible to create views in code first approach? if yes please let me know.

    Regards,

    Mariappan .S


    Tuesday, September 20, 2011 11:49 AM

Answers

  • Hello Mariappan,

    Thanks for your post.

    As far as I know, 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 still want to create views, you could manually execute the create view SQL query in your code. Just like execute Entity SQL. 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.

    This answer was given by Ladislav Mrnka at here: http://stackoverflow.com/questions/5889905/how-do-i-define-a-database-view-using-entity-framework-4-code-first

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 6:42 AM
    Moderator

All replies

  • Hello Mariappan,

    Thanks for your post.

    As far as I know, 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 still want to create views, you could manually execute the create view SQL query in your code. Just like execute Entity SQL. 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.

    This answer was given by Ladislav Mrnka at here: http://stackoverflow.com/questions/5889905/how-do-i-define-a-database-view-using-entity-framework-4-code-first

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 6:42 AM
    Moderator
  • Hi

    Thanks for your reply , My real requirement was , for example take product and customer both are related. I have two table one for customer and another one for product , both have relationship. I want to get some details like , how many customers brought product A( Product A sold to 100 customers) same like some statistical data. 

    what we did previously means we created view. That view contains business log for stats data and later the view was attached as one entity .(this is possible in entity model because we have options to select table and views. 

    Like that have some view , can i attache those views as entity on code first approach. So that some data service operation get stats from view.

     

     

     

    Monday, October 3, 2011 11:40 AM