簡體   English   中英

如何在 OnModelCreating 期間將自定義數據添加到 EF Core 模型?

[英]How to add custom data to an EF Core model during OnModelCreating?

我正在嘗試編寫一個在模型構建階段執行一些額外檢查的方法,然后將自定義對象CustomInfoModel存儲在模型中,以后可以在需要時將其拉回。

CustomInfoModel包含特定於模型本身的數據,因此我不想在每次創建 DbContext 時都構建它。

所以我的想法是在ModelBuilder上創建一個擴展方法並從OnModelBuilding調用它。 似乎注釋是存儲此對象的最佳方式,因此我創建了該對象,將其存儲為注釋,然后調用Ignore以便它不會被使用:

public void AddCustomInfoModel(this ModelBuilder modelBuilder)
{
    var customModel = CustomInfoModel.FromBuilder(modelBuilder);
    modelBuilder.HasAnnotation(nameof(CustomInfoModel), customModel);
    modelBuilder.Ignore<CustomInfoModel>();
}

這似乎工作正常; 我可以稍后在需要時使用Model.FindAnnotation()檢索此對象。 但是,當嘗試使用dotnet ef migrations add時,我收到以下錯誤:

System.InvalidOperationException: Cannot scaffold C# literals of type 'MyApp.CustomInfoModel'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Fragment(MethodCallCodeFragment fragment, Boolean typeQualified, String instanceIdentifier, Int32 indent)
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Fragment(MethodCallCodeFragment fragment, String instanceIdentifier, Boolean typeQualified)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotations(String builderName, IAnnotatable annotatable, IndentedStringBuilder stringBuilder, Dictionary`2 annotations, Boolean inChainedCall, Boolean leadingNewline)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String modelBuilderName, IModel model, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMetadata(String migrationNamespace, Type contextType, String migrationName, String migrationId, IModel targetModel)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Cannot scaffold C# literals of type 'MyApp.CustomInfoModel'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.

所以我的問題是:有沒有辦法隱藏這個注釋,讓數據庫提供者完全忽略它? 或者有沒有更好的方法在模型中存儲“額外”數據,這些數據只創建一次,然后可供所有未來的DbContext實例使用?

從代碼優先的角度來看, 文檔通過實現IMigrationsSqlGenerator的替換服務來說明類似的自定義示例,該替換服務擴展了您計划用於部署的任何特定於提供程序的實現。 也就是說,您將獲得一個覆蓋方法,用於微調給定提供程序對您的自定義數據/操作的處理。

或許您可以檢查 EF Core 源代碼以查找允許您自定義腳手架遷移過程的腳手架對應項? 或者在他們的 GitHub 上打開一個問題來詢問這樣的 API / 請求他們擴展 API 以包含它(如果它不存在)(他們似乎非常敏感)?

抱歉,我無法提供更多幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM