When doing an EF data migration, we have a bunch of json with pre set guids like this:
{
"Id": "61dcc24e9b524f10b69a5c3f17be8603",
"MakeName": "AUDI",
"ExternalId": "61dcc24e9b524f10b69a5c3f17be8604",
"CreatedBy": "System",
"CreatedOn": "2022/01/05"
},
{
"Id": "27a617d75b2e45bab513e2f336fcd921",
"MakeName": "BMW",
"ExternalId": "27a617d75b2e45bab513e2f336fcd927",
"CreatedBy": "System",
"CreatedOn": "2022/01/05"
},
Make Class
public class Make : AuditableEntity
{
public Make() { }
Guid Id { get; }
public String MakeName { get; set; }
public String CreatedBy { get; set; } = null!;
public DateTimeOffset CreatedOn { get; set; }
Guid ExternalId{ get; set; }
}
We then use a generic seed function to extract the data:
public static List<TEntity> SeedFromJson<TEntity>(string fileName)
{
string path = "../path/Seeds";
string fullPath = Path.Combine(path, fileName);
var result = new List<TEntity>();
using (StreamReader reader = new StreamReader(fullPath))
{
string json = reader.ReadToEnd();
result = JsonConvert.DeserializeObject<List<TEntity>>(json);
}
return result;
}
The result is used in a custom config like this:
builder.HasData(LargeDataHelper.SeedFromJson<Make>("Makes.json"));
The issue is that when we run the migration. The following error is shown:
The seed entity for entity type 'Make' cannot be added because a default value was provided for the required property 'Id'. Please provide a value different from '00000000-0000-0000-0000-000000000000'.
It seems like when debugging the result = JsonConvert.DeserializeObject
replaces the guid provided with zeros.