how to generate views initially so that we can boost the performace. RRS feed

  • Question

  • User2102072086 posted


       I have noticed an article "Pre-Generated Mapping Views", which was not clear. please send me some article for

    vs 2013 and ef6(onwards).

    yours sincerely

    Thursday, May 3, 2018 2:00 PM

All replies

  • User2053451246 posted

    If you use Entity Framework, by default every time your application starts it will have to spend time to compile your EF models into SQL code that is sent to the database to get results.  By per-compiling the view code you save on the time it takes for your application to start.

    For instance if you were to use https://marketplace.visualstudio.com/items?itemName=EntityFrameworkTeam.EntityFrameworkPowerToolsBeta4 it has a feature that allows you to right-click a DB Context .cs file and generate another .cs file with all of the per-compiled views.  (The page does say beta but I used it for several years; the non-beta one available today is not available for VS 2013).  It generates this for a table called State:

            private static DbMappingView GetView29()
                return new DbMappingView(@"
        SELECT VALUE -- Constructing State
            [CodeFirstDatabaseSchema.State](T1.State_StateId, T1.State_Name, T1.State_Description)
        FROM (
                T.StateId AS State_StateId, 
                T.Name AS State_Name, 
                T.Description AS State_Description, 
                True AS _from0
            FROM QuotingContext.States AS T
        ) AS T1");

    The file will be called YourContextName.Views.cs.  EF will automatically use the file at startup because of it's name (by convention).

    Thursday, May 3, 2018 5:19 PM
  • User1724605321 posted

    Hi rajemessage ,

    Mapping view generation is a significant part of the overall cost of executing the first query, the Entity Framework enables you to pre-generate mapping views and include them in the compiled project. For more information, see Performance Considerations (Entity Framework).

    As @Ryan suggested , you could create Mapping Views with the EF Power Tools  . Please refer to below article which provides the detail steps with code sample :


    Best Regards,

    Nan Yu

    Friday, May 4, 2018 3:02 AM