-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-nullable json array fields are translating to nullable in migration files #35540
Comments
Confirmed, owned JSON arrays seem to always be nullable. Minimal repro on SQL Server: Minimal reprousing System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
await using var context = new MyContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
public class MyContext : DbContext
{
public DbSet<Table> Tables { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Table>()
.OwnsMany(e => e.DataArray, b => b.ToJson("data_array"));
modelBuilder.Entity<Table>()
.OwnsOne(e => e.DataObject, b => b.ToJson("data_object"));
}
}
public class Table
{
public int Id { get; set; }
public TableData[] DataArray { get; set; }
public TableData DataObject { get; set; }
}
public class TableData
{
public string SomeData { get; set; }
} Putting on the backlog for now. The workaround is to manually make the column non-nullable in migrations after creation. |
you can explicitly set the navigation as required: modelBuilder.Entity<Table>().Navigation(x => x.DataArray).IsRequired(); However, this throws an exception (CoreStrings.NonUniqueRequiredDependentNavigation):
So this seems to be by design, at least when JSON is implemented as owned types. @AndriySvyryd |
I think we're conflating requiredness of the navigation in the model (I think collection navigations are indeed never required, you can always have zero) with requiredness of the JSON column holding the collection. Even if there are zero owned entity types, the database representation of that (I think) is still a non-null JSON column with an empty JSON array string representation ( |
Yeah, looks like a bug |
Bug description
I think there is an issue, but i don't know if this issue related to this repo or to npgsql. When i describe a column field as
Object[]
with default value[]
and creating a migration - this field in a migration file described as nullable unlike i was expect. I have a simple repro example. When i rundotnet ef migrations add Test
it generate following migration file:If it is not an issue can you show me any workarounds to describe
data_array
as non nullable?Your code
Stack traces
Verbose output
EF Core version
8.0.11
Database provider
Npgsql.EntityFrameworkCore.PostgreSQL
Target framework
.NET 8.0
Operating system
Ubuntu 22.04.4 LTS
IDE
JetBrains Rider 2024.1.3
The text was updated successfully, but these errors were encountered: