It makes a few hour that I'm trying to figure this one out, but could find a solution.
I have an entity with a List<string>
property and I'd like to data seeding it. I succeeding using modelBuilder.Entity<T>.HasData()
, but it doesn't work with dotnet ef migrations
.
Here is my entity:
public class MyEntity
{
public int Id { get; set; }
public List<string> Names { get; set; }
}
And here's my DbContext.OnModelCreating
override:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.HasData(
new MyEntity { Id = 1, Names = new List<string> { "text1", "text2" } },
new MyEntity { Id = 2, Names = new List<string> { "text1", "text2", "text3" } },
new MyEntity { Id = 3, Names = new List<string> { "text1", "text2" } },
new MyEntity { Id = 4, Names = new List<string> { "text1", "text2" } },
new MyEntity { Id = 5, Names = new List<string> { "text1", "text2", "text3" } });
}
This works if I run my app, it creates the table and seed it with the wanted values. But if I run dotnet ef migrations add AddMyEntities --project src/MyProject --startup-project src/MyProject.Api
, I get this output:
Build started...
Build succeeded.
System.NotSupportedException: The type mapping for 'List<string>' has not implemented code literal generation.
at Microsoft.EntityFrameworkCore.Storage.CoreTypeMapping.GenerateCodeLiteral(Object value)
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(Object[,] values)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(InsertDataOperation operation, IndentedStringBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(String builderName, IReadOnlyList`1 operations, IndentedStringBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMigration(String migrationNamespace, String migrationName, IReadOnlyList`1 upOperations, IReadOnlyList`1 downOperations)
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)
The type mapping for 'List<string>' has not implemented code literal generation.
How can I seed a simple List<string>
property?
Edit: I think this question is different from other questions such as Entity Framework - Code First - Can't Store List because my program works fine if I launch it on a blank DB (using PostgreSQL, it creates a Names
column of type text[]
, as you would expect). The problem is when I use the dotnet ef
migration tool.