-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap SQL for HasServiceTier and HasPerformanceLevel to be ignored when not on Azure #20682
Comments
@thPion The best way to approach this would be to only include these in the model in OnModelCreating if the application is targeting Azure. This can usually be determined from the connection string. That being said, I would consider not having EF create Azure databases automatically, but instead use the portal to provision the databases you need such that you have complete control over what kind of database is being used. Migrations can still be used to populate the schema of a database that exists but is empty. |
Making it optional in OnModelCreating is not doing anything since that's not the code that throws the Exception. That's in the migration. If you can show me an elegant way to determine which DB I'm on from inside a connection, that would be helpful. |
@thPion You are correct; my answer was not useful. I'll discuss with the team. We introduced these so that people who are creating the database from code have a way to set these things, but we still recommend using the portal to create your Azure database so that you are fully aware of what you are getting, and won't get inappropriate defaults without realizing it. (The fact that the default for these things changed silently under the covers was, somewhat ironically, a motivating factor both to add this feature and at the same time recommend not using it.) |
Thanks for revisiting the issue! So what you are saying is, you recommend doing it through the portal but added theses features for people who have to do it via code for one or another reason. Gotcha, thanks for clearing that up. |
@thPion We discussed this again and we're going to start wrapping this in a check for Azure. For now, you can edit the migration to do this manually with something like: migrationBuilder.Sql(@"IF SERVERPROPERTY('EngineEdition') = 5
EXEC(N'ALTER DATABASE [ThreeOne.SomeDbContext] MODIFY (EDITION = ''Basic'', SERVICE_OBJECTIVE = ''Basic'' );');
"); |
Which namespace are HasServiceTier and HasPerformanceLevel in? Can't find them anywhere. Amazed more people are not looking for this functionality, since Microsoft made GEN5 the default database size, this has been a real issue for us. |
namespace Microsoft.EntityFrameworkCore |
Thanks - I have that installed but still can't see them? Just got off a call with a Microsoft engineer and he couldn't see them either. |
Are you using the correct version of EF Core? They got introduced in 3.1 and are not present in earlier versions. |
Then I have no idea. It's this one: |
@redowl3 Please open a new issue and post the csproj for your project that is not finding these APIs. |
If it helps to anyone, we are using EFCore migrations to generate databases in different environments. In order to generate an ALTER statement before each migration we use a custom SqlServerMigrationGenerator that replaces the original one by using Then in MyMigrationGenerator we can override the Generate function as
Of course a production ready solution can be extended by checking the model ( |
When using HasServiceTier and HasPerformanceLevel in OnModelCreating a migration can be created that looks like this:
If applied to a non-Azure database this causes an Exception. I see two problems with this
Steps to reproduce
Add the following lines to OnModelCreating:
Then use
Add-Migration
to create a migration and try to apply it to a local SQL Server instance.You'll see an Exception like this:
I also asked for help in SO:
https://stackoverflow.com/questions/61205031/specify-azure-sql-server-edition-in-ef-core-without-breaking-local-development
Further technical details
EF Core version: 3.1.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Win 10
IDE: Visual Studio 2019 16.5.4
The text was updated successfully, but these errors were encountered: