locked
How to detect EF view generation RRS feed

  • Question

  • Hi, i would to know if is possible to detect when EF generate views instead of using the generated views class compiled in the assembly.

    Is there any flag/setting somewhere to trace the view generation or is there any event that i could subscribe? (only for debug purpose)

    Best regards Max


    Thursday, May 17, 2012 3:15 PM

All replies

  • Hi iL MaX;

    Not sure what you mean by, "EF generate views instead of using the generated views class compiled in the assembly.", Do you mean that you want to see what SQL statements are being set to the SQL server? if so then the following will do that.

    // Sample code is for the Northwind sample db 
    // The variable query will have the SQL sent to the DB 
    // for the query results
    using( var ctx = new NorthwindEntities())
    {
    	var results = from c in ctx.Customers
        	          where c.City == "London"
            	      select c;
                  
    	var query = ((ObjectQuery)(results)).ToTraceString();
    	
    	Console.Writeline(query);
    	
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 17, 2012 4:29 PM
  • Hi Fernando, thanks for the answer.

    I don't want to see the generated sql, i want to know if EF uses the view class generated by me and included in the project or not.

    according to this view generation take place dynamically once and then is cached if not pre-generated, but i'm seeing some cases where EF don't uses pre-generated views, so i'm asking how to detect when EF start building view dynamically.

    Max


    Thursday, May 17, 2012 6:07 PM
  • Sorry this look like something new in EF 5 which is only out in beta and I am not familiar with this as of yet.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 17, 2012 6:47 PM
  • View generation is in EF4 as well, and (but i don't know for sure) even from EF1
    Thursday, May 17, 2012 6:48 PM
  • No one?
    Friday, May 18, 2012 10:53 AM
  • Hi iL.MaX,

    Welcome to MSDN Forum.

    I'm afraid we can't detect the view generation. In the article you posted, you can find view generation is a step of query execution after metadata loading, we can't be informed when it is generated.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 21, 2012 2:55 AM
  • Hi iL.MaX,

    Any update? If you need further help, please feel free to let me know, I will be more than happy to be of assistance. :)

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 24, 2012 5:46 AM
  • Hi Allen,

    the problem was that with a very big EF model with many tables and relations, the first query never starts (after 5 min i killed the process) so i ended up with pre-generating views.

    After adding the views, the result was the same, the views were not used even the class was present in the same assembly were the context class is, the only method i found for forcing EF to use the views was to call the MetadataWorkspace.LoadFromAssembly method passing in the assembly where the context is defined rigth after creating the context e.g.

    var context = new MyContext(new EntitiConnection(myMetadataStrings, someDbConnection));
    context.MetadataWorkspace.LoadFromAssembly(typeof(MyContext).Assembly);

    Doing this ways seems to use pre-generated views but it's really strange for me because myMetadataStrings already points only to that assembly, so why i need to load Metadata info from the same assembly?

    P.S. In my configuration I have an assembly for the Context and many assemblies for the entities.

    Maybe a bug or am i missing something?


    Thursday, May 24, 2012 9:02 PM
  • Doing this ways seems to use pre-generated views but it's really strange for me because myMetadataStrings already points only to that assembly, so why i need to load Metadata info from the same assembly?

    P.S. In my configuration I have an assembly for the Context and many assemblies for the entities.

    @Massimiliano Donini: I believe your problem is caused by the fact that the pregenerated view classes are searched for (don't ask me why) in the assembly of the first entity listed in the relevant DbContext, not the assembly of the context itself.  I described a workaround using this behavior here: http://stackoverflow.com/a/12060962/237091

    Monday, January 28, 2013 8:29 PM