Skip to content

Commit

Permalink
Treat required owned entities on derived types as optional.
Browse files Browse the repository at this point in the history
Fixes #31107
  • Loading branch information
AndriySvyryd committed Oct 4, 2023
1 parent 4be0967 commit 1c83dc3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public sealed partial class SelectExpression : TableExpressionBase

private static readonly bool UseOldBehavior30273
= AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue30273", out var enabled30273) && enabled30273;
private static readonly bool UseOldBehavior31107 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31107", out var enabled31107) && enabled31107;

private static readonly IdentifierComparer IdentifierComparerInstance = new();

Expand Down Expand Up @@ -2637,7 +2639,9 @@ static IReadOnlyDictionary<IProperty, ColumnExpression> GetPropertyExpressions(
var parentNullable = identifyingColumn.IsNullable;
var pkColumnsNullable = parentNullable
|| (derivedType && ownerType.GetMappingStrategy() != RelationalAnnotationNames.TphMappingStrategy);
var newColumnsNullable = pkColumnsNullable || !navigation.ForeignKey.IsRequiredDependent;
var newColumnsNullable = pkColumnsNullable
|| !navigation.ForeignKey.IsRequiredDependent
|| (derivedType && !UseOldBehavior31107);
if (derivedTpt)
{
principalMappings = principalMappings.Except(ownerType.BaseType!.GetViewOrTableMappings().Select(e => e.Table));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,27 +408,27 @@ public void Seed()
}
}

public class RotRutCase
protected class RotRutCase
{
public int Id { get; set; }
public string Buyer { get; set; }
public Rot Rot { get; set; }
public Rut Rut { get; set; }
}

public class Rot
protected class Rot
{
public int? ServiceType { get; set; }
public string ApartmentNo { get; set; }
}

public class RotDto
protected class RotDto
{
public int? MyServiceType { get; set; }
public string MyApartmentNo { get; set; }
}

public class Rut
protected class Rut
{
public int? Value { get; set; }
}
Expand Down Expand Up @@ -498,26 +498,84 @@ public void Seed()
}
}

public class Monarch30358
protected class Monarch30358
{
public int Id { get; set; }
public string Name { get; set; }
public string RulerOf { get; set; }
}

public class Magus30358
protected class Magus30358
{
public int Id { get; set; }
public string Name { get; set; }
public string Affiliation { get; set; }
public MagicTool30358 ToolUsed { get; set; }
}

public class MagicTool30358
protected class MagicTool30358
{
public string Name { get; set; }
}

protected abstract class BaseEntity31107
{
public Guid Id { get; set; }
}

protected sealed class ChildData31107
{
public Guid Id { get; set; }
}

protected sealed class Child1Entity31107 : BaseEntity31107
{
public ChildData31107 Data { get; set; }
}

protected sealed class Child2Entity31107 : BaseEntity31107
{
}

[ConditionalFact]
public async Task Can_have_required_owned_type_on_derived_type()
{
var contextFactory = await InitializeAsync<RequiredNavigationContext>(seed: c => c.Seed());
using var context = contextFactory.CreateContext();

context.Set<BaseEntity31107>().ToList();
}

private class RequiredNavigationContext : DbContext
{
public RequiredNavigationContext(DbContextOptions options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BaseEntity31107>();
modelBuilder.Entity<Child1Entity31107>(b =>
{
b.OwnsOne(entity => entity.Data, builder =>
{
builder.ToTable("Child1EntityData");
builder.WithOwner().HasForeignKey("Child1EntityId");
});
b.Navigation(e => e.Data).IsRequired();
});

modelBuilder.Entity<Child2Entity31107>();
}
public void Seed()
{
Add(new Child2Entity31107 { Id = Guid.NewGuid() });

SaveChanges();
}
}

protected override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base.AddOptions(builder).ConfigureWarnings(
c => c
Expand Down

0 comments on commit 1c83dc3

Please sign in to comment.