Do not map annotation to database during migrations
See original GitHub issueAsk a question
I’ve read the docs, searched StackOverflow and tried my best to dig into past issues. The best I could find was this but the method signatures seem to have changed since then.
I am trying to understand the “annotations” feature but it is undocumented.
When building a model I use this:
MyAnnotation value = GetSomeComplexObjectToUseAsAnnotation();
entityTypeBuilder
.Property(x => x.Name)
.HasAnnotation("SomeKey", value);
And later I can retrieve it using something like this:
context
.Model
.GetEntityTypes()
.SelectMany(x => x.GetProperties())
.Select(x => x.FindAnnotation("SomeKey")?.Value)
.Where(x => x is MyAnnotation);
I use it as a way to pass around info concerning the model, which I can use for various purposes. That works!
But dotnet ef migrations add
throws:
System.InvalidOperationException: Cannot scaffold C# literals of type ‘MyAnnotation’. 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.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotation(IAnnotation annotation, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotations(IEnumerable
1 annotations, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GeneratePropertyAnnotations(IProperty property, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperty(String builderName, IProperty property, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperties(String builderName, IEnumerable
1 properties, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList1 entityTypes, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String builderName, 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)
I assume the problem is the migration system is expecting a primitive type, but it gets an instance of MyAnnotation
and doesn’t know how to map that to the database - but I don’t want it in the database.
In the post I linked above ajcvickers said:
since many annotations only affect runtime behavior, not the database schema.
So I assume (?) I can use the annotation system this way. How do I tell migrations to not map that annotation?
Include provider and version information
EF Core version: 5.0.11 Database provider: sqlite, postgres Target framework: dotnet5 Operating system: linux IDE: vscode 1.63.0
Thank you!
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Looks like you should be able to replace the IAnnotationCodeGenerator service and override AnnotationCodeGenerator.FilterIgnoredAnnotations to add exclusions.
LOL you schooled me!
The docs say
So I fiddled with those and presumably that was the problem. I’ve since learned I don’t need to change anything other than the filter method, as you explained.
This very simple code works, for anyone who needs it:
Thanks guys!