Play ASP.NET Monsters #1: Startup.cs

The Discussion

  • User profile image

    Can fix the video and make it bigger so it fits in the video player? Tough to see the demo.

  • User profile image

    I noticed inside of your Configure method when you were getting the database context, you first got the IServiceScopeFactory before pulling back your ApplicationDbContext so that you could call the Migrate method. Was there a reason for doing it this way? In code that I have written, I have just directly made a call in the Configure method that puts this all into one line like so "app.ApplicationServices.GetRequiredService<ApplicationDbContext>();". This there a condition where doing it the way you have it is better than just directly getting it from the ApplicationServices? Also, I can relate to your empty catch on the Migrate call. One thing I have done since I noticed I got this when not using a SQL backed database (like the InMemoryDatabase) is I made a very specific catch statement to only ignore the case when the Migrate method wasn't supported, but let's me know if I get something else. What I have done is created a catch block like so "catch (InvalidOperationException e) when (e.Message == "No service for type 'Microsoft.Data.Entity.Migrations.IMigrator' has been registered.") {}" which is still empty, but at least only ignores exceptions around the IMigrator not being implemented (I know, it is very English specific, I could probably clean it up a bit but it works for projects I'm writing at the moment).

    Nice series, keep up the good work.

  • User profile image

    @Oliver:Sorry about that...we're still learning the ropes. We hope we have it figured out for future episodes!

  • User profile image

    @Stokes:Thanks for the questions and comments. 

    Here we are just looking into the default startup, so there's no specific reason why that code is there, other than someone responsible for creating the template thought it should be like so. We'll be doing a number of posts on services and DI in general, and we'll try to explore that a little deeper.

    And, yeah, UGH on those empty try...catches, eh? Thanks for sharing your approach.


  • User profile image

    @Stokes @CanadianJames

    I believe this pattern came out of the EF team's need to have a proper "seed" method/process.

    You shouldn't apply a using statement on a dbcontext whose lifetime is owned by DI.

    Getting the context via a service scope is a safter approach.

    Great series BTW...

  • User profile image

    Great Start.cs to the series! 

  • User profile image

    Unwatchable on a smaller tablet. Don't forget to zoom. :)

  • User profile image

    Interesting Russian Doll analogy. I don't like Russian Dolls. They're so full of themselves.

  • User profile image

    I have VS 2013 and my MVC template is quite different...
    Is there a way for me to see this as displayed without upgrading to the latest VS?

  • User profile image

    Sorry, didn't sign in first; I am BrianS!

  • User profile image

    It is hard to see

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.