SQL Server How to make owned type non-nullable?

eufgjt7s  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(100)

I have a Location entity. It has a collection of opening hours and it should be stored as a JSON in the database.

public class Location
{
    public Guid Id { get; set; }
    public required string Name { get; set; }
    public ICollection<OpeningHours> OpeningHours { get; set; } = new List<OpeningHours>();
}

public class OpeningHours
{
    public DayOfWeek Day { get; set; }
    public bool IsOpen24Hours { get; set; }
    public ICollection<TimeInterval> Intervals { get; set; } = new List<TimeInterval>();
}

public class TimeInterval
{
    public TimeOnly Start { get; set; }
    public TimeOnly End { get; set; }
}

and below is my configuration

internal class LocationConfiguration : IEntityTypeConfiguration<Location>
{
    public void Configure(EntityTypeBuilder<Location> builder)
    {

        builder.OwnsMany(
                l => l.OpeningHours, ownedNavigationBuilder =>
                {
                    ownedNavigationBuilder.ToJson();
                    ownedNavigationBuilder.OwnsMany(o => o.Intervals);
                    ownedNavigationBuilder.WithOwner();
                });
    }
}

No matter what when adding new migration it makes OpeningHours nullable. I want to make it non-nullable in the database

qxsslcnc

qxsslcnc1#

You can try using the .IsRequired() method when configuring the OpeningHours property.

internal class LocationConfiguration : IEntityTypeConfiguration<Location>
{
    public void Configure(EntityTypeBuilder<Location> builder)
    {
        builder
            .Property(l => l.Name)
            .IsRequired(); // Assuming Name is required

        builder.OwnsMany(
                l => l.OpeningHours, ownedNavigationBuilder =>
                {
                    ownedNavigationBuilder.ToJson();
                    ownedNavigationBuilder.OwnsMany(o => o.Intervals);
                    ownedNavigationBuilder.WithOwner();
                });
    }
}

Make sure that the Name property in the Location class is marked as required if it is indeed a required field.

Generate a new migration to apply the changes to your database:

dotnet ef migrations add UpdatedLocationConfiguration

Then, update the database:

dotnet ef database update

Second implementation

  • In your Location class, you can use the [Required] attribute to specify that the OpeningHours collection is required.
public class Location
{
     public Guid Id { get; set; }

     [Required]
     public string Name { get; set; }

     public ICollection<OpeningHours> OpeningHours { get; set; } = new List<OpeningHours>();
}
  • In your DbContext class, you can override the OnModelCreating method and specify the configuration for OpeningHours using HasMany and WithOne .
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Location>()
        .HasMany(l => l.OpeningHours)
        .WithOne();
}
  • Ensure that you have a correct JSON converter registered for EF Core. This can be done using the UseJson extension method.
ownedNavigationBuilder.Property(o => o.Intervals)
     .HasConversion(
         v => JsonSerializer.Serialize(v, null),
         v => JsonSerializer.Deserialize<ICollection<TimeInterval>>(v, null)
     )
     .HasColumnType("jsonb"); // Use the appropriate SQL type for your database

相关问题