Azure SQL 資料庫選項
Note
您在連線到 Azure SQL 時,應該 使用 UseAzureSql 方法 而不是 UseSqlServer。
Azure SQL Database 提供通常透過 Azure 入口網站設定 的各種定價選項 。 不過,如果您使用 EF Core 移轉來管理架構,您可以使用 EF 設定所需的選項,並在 EF 建立資料庫時套用這些選項。
您可以使用 HasServiceTier 來指定資料庫 (EDITION) 的服務層級:
modelBuilder.HasServiceTier("BusinessCritical");
您可以使用 HasDatabaseMaxSize 來指定資料庫的大小上限:
modelBuilder.HasDatabaseMaxSize("2 GB");
您可以使用 HasPerformanceLevel 來指定資料庫的效能層級 (SERVICE_OBJECTIVE:
modelBuilder.HasPerformanceLevel("BC_Gen4_1");
使用 HasPerformanceLevelSql 來設定彈性集區,因為值不是字串常值:
modelBuilder.HasPerformanceLevelSql("ELASTIC_POOL ( name = myelasticpool )");
Tip
您可以在 ALTER DATABASE 檔中找到所有支援的值。
SaveChanges、觸發器與 OUTPUT 子句
當 EF Core 將變更儲存到資料庫時,會採用 T-SQL OUTPUT 子句的優化技術。 不幸的是,OUTPUT 子句有一些限制;值得注意的是,它無法與具有觸發器的表格一起使用。
若遇到與 OUTPUT 子句使用相關的限制,可透過以下方式 UseSqlOutputClause在特定表格中停用:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.ToTable(tb => tb.UseSqlOutputClause(false));
}
這樣做會讓 EF 改用較舊且效率較低的更新表格技術。
如果你的大部分或全部資料表都有觸發器,你可以透過以下模型 構建約定來設定所有模型的資料表:
public class NoOutputClauseConvention : IModelFinalizingConvention
{
public virtual void ProcessModelFinalizing(
IConventionModelBuilder modelBuilder,
IConventionContext<IConventionModelBuilder> context)
{
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
{
var table = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table);
if (table is not null)
{
entityType.Builder.UseSqlOutputClause(false);
}
foreach (var fragment in entityType.GetMappingFragments(StoreObjectType.Table))
{
entityType.Builder.UseSqlOutputClause(false, fragment.StoreObject);
}
}
}
}
這實際上會對您所有模型的數據表執行 UseSqlOutputClause,而非需要您手動針對每個數據表執行此操作。