Difference between revisions of "Best Practice MS SQL"

From MgmtWiki
Jump to: navigation, search
(Issues)
 
Line 5: Line 5:
 
*see https://go.microsoft.com/fwlink/?linkid=851728
 
*see https://go.microsoft.com/fwlink/?linkid=851728
 
You get that error because to generate migrations you need either:
 
You get that error because to generate migrations you need either:
 
+
#A DbContext with a default constructor (that is, a parameterless constructor)
A DbContext with a default constructor (that is, a parameterless constructor)
+
#Being able to get the DbContext from ApplicationServices (that is, Dependency Injection)
Being able to get the DbContext from ApplicationServices (that is, Dependency Injection)
+
#A design time factory that returns a properly configured DbContext.
A design time factory that returns a properly configured DbContext.
 
 
Since ApplicationDbContext constructor has a parameter, you have to use options 2 or 3. See the details in the article: https://docs.microsoft.com/ef/core/miscellaneous/cli/dbcontext-creation
 
Since ApplicationDbContext constructor has a parameter, you have to use options 2 or 3. See the details in the article: https://docs.microsoft.com/ef/core/miscellaneous/cli/dbcontext-creation
  

Latest revision as of 14:22, 27 July 2020

Full Title

Best Practices and Issues with Microsoft SQL Server

Issues

Unable to create an object of type 'ServiceDbContext'. For the different patterns supported at design time

You get that error because to generate migrations you need either:

  1. A DbContext with a default constructor (that is, a parameterless constructor)
  2. Being able to get the DbContext from ApplicationServices (that is, Dependency Injection)
  3. A design time factory that returns a properly configured DbContext.

Since ApplicationDbContext constructor has a parameter, you have to use options 2 or 3. See the details in the article: https://docs.microsoft.com/ef/core/miscellaneous/cli/dbcontext-creation

But option 2 (the one originally used) doesn't work any more, because the program startup was changed to improve logging.

And, since there's no DesignTimeFactory for ApplicationDbContext you just won't be able to create migrations at this time.

If you want to learn about this way for creating migrations you can:

Create an ApplicationDbContextDesignTimeFactory, similar to CatalogContextDesignFactory or IntegrationEventLogContextDesignTimeFactory, or Try with other microservice like Catalog.API, that already has its DesignTimeFactory.

References