dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.6k stars 1.95k forks source link

Document updating a JSON mapped column with a required property #4514

Open TimSalomons opened 10 months ago

TimSalomons commented 10 months ago

I have a type which already exists in my database. We've expanded this type and added new required properties to it. However this causes the deserialization to fail since these properties do no yet exist on existing rows.

Below is a small example of my scenario. How would I go about updating the existing rows/data to match the new contract? Setting default values does not seem to help since the deserialization process will fail before it.

public class MyClass {
    // registered as a json column inside the dbcontext
    public required List<MyRecord> Records { get; set; }
}

public record MyRecord
{
    public required Guid Identifier { get; set; }
    public required string Title { get; set; }

   // Below properties are new
    public required bool IsDeleted { get; set; } = false;
    public required DateTimeOffset? DeletedAt { get; set; } = null;

provider and version information

EF Core version: 7.0.11 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 7.0 Operating system: Win11 IDE: Rider 2023.2.1

maumar commented 10 months ago

If you want to use EF start to finish, you can make the new properties optional at first, then load all the entities, add values for those new properties, save changes and then make the properties required.

ajcvickers commented 10 months ago

Note from triage: document