Discussion Using data view in EF 4.1 code first.

  • Wednesday, August 01, 2012 11:38 AM
     
     

    Hi All,

    I have an SQL view and I want to use it with Entity Framework 4.1 Code first.

    I know that view can we use as table in Entity Framework but my problem is SQL views are customizable, User can add or delete view column. How to manage views in EF 4.1 code first mode ?

    Please help.

    Thanks in advance

    MK

All Replies

  • Thursday, August 02, 2012 3:15 AM
    Moderator
     
     

    Hi Sanjeev,

    Thank you for your post.

    Please take a look at this page: http://blogs.msdn.com/b/rickandy/archive/2008/10/04/how-to-create-an-updateable-view-with-ado-entity-framework.aspx

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

  • Saturday, August 04, 2012 12:34 PM
     
     

    Hi Alexander,

    Thanks for your reply.

    I did not understand these steps.

    Edit the EF SSDL, comment out the <DefiningQuery> , remove store: prefix from Schema="SalesLT"  and remove store:Name="vAddr" . The commented/changed code below

    <EntitySet Name="Address" EntityType="AdventureWorksLT2008Model.Store.Address" store:Type="Tables" Schema="SalesLT" />

    <EntitySet Name="vAddr" EntityType="AdventureWorksLT2008Model.Store.vAddr" store:Type="Views" Schema="SalesLT" />

    Can you explain me more.

    Thanks in advance

  • Monday, August 06, 2012 7:31 AM
    Moderator
     
     

    Hi Sanjeev,

    If you use Database first, you can open .edmx file with a XML editor. Then you can find the SSDL part in this XML file. As you want to use Code First to map to a view, just tell Code First that it's a table and it will use the same SQL against the view that it would for a table. Obviously if your view isn't writeable then saving is going to fail if you try and update values in the entities that are based on the view, but we will just delegate to the database so if you don't update these entities then you won't have any issues. Please refer to this page: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/f154595c-717e-4703-a81d-ee63633a481e

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

  • Friday, August 10, 2012 1:21 PM
     
     

    Hi Alexander,

    Sorry to late reply.  Thanks for your reply. I have some confusion about view in EF code first mode.

    As per EF code first model, we required one entity class to access view in EF4.1 code first mode. An entity class contains metadata of view. My question is how to manage entity class at the time of view is changed?  Because user can change metadata  of view at database side.

    Can you provide me all the steps for using predefined customizable view in EF4.1 code first mode. That is very help full for me.

    Thanks in advance.

  • Thursday, August 16, 2012 5:18 AM
    Moderator
     
     

    Hi Sanjeev,

    For your concern, I afraid you are required to update your code to get the right mapping of changed database.

    In addition, you can take a look at this page: http://stackoverflow.com/questions/5107879/ef-code-first-sql-server-views

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us