Skip to content

[5.0.0-rc2] Add null checks to account for owned types in TPT #22587

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/EFCore.Relational/Extensions/RelationalForeignKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,18 @@ public static string GetDefaultName(
.SelectMany(fk => fk.PrincipalEntityType.GetForeignKeys()))
{
if (principalStoreObject.Name == otherForeignKey.PrincipalEntityType.GetTableName()
&& principalStoreObject.Schema == otherForeignKey.PrincipalEntityType.GetSchema()
&& propertyNames.SequenceEqual(otherForeignKey.Properties.GetColumnNames(storeObject))
&& principalPropertyNames.SequenceEqual(
otherForeignKey.PrincipalKey.Properties.GetColumnNames(principalStoreObject)))
&& principalStoreObject.Schema == otherForeignKey.PrincipalEntityType.GetSchema())
{
linkedForeignKey = otherForeignKey;
break;
var otherColumnNames = otherForeignKey.Properties.GetColumnNames(storeObject);
var otherPrincipalColumnNames = otherForeignKey.PrincipalKey.Properties.GetColumnNames(principalStoreObject);
if (otherColumnNames != null
&& otherPrincipalColumnNames != null
&& propertyNames.SequenceEqual(otherColumnNames)
&& principalPropertyNames.SequenceEqual(otherPrincipalColumnNames))
{
linkedForeignKey = otherForeignKey;
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,10 @@ private void ApplyOnRelatedEntityTypes(IConventionEntityType entityType, IConven
foreach (var relatedEntityType in relatedEntityTypes)
{
var relatedEntityTypeBuilder = relatedEntityType.Builder;
DiscoverRelationships(relatedEntityTypeBuilder, context);
if (relatedEntityTypeBuilder != null)
{
DiscoverRelationships(relatedEntityTypeBuilder, context);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Xunit;

namespace Microsoft.EntityFrameworkCore
{
public abstract class DataAnnotationRelationalTestBase<TFixture> : DataAnnotationTestBase<TFixture>
where TFixture : DataAnnotationRelationalTestBase<TFixture>.DataAnnotationRelationalFixtureBase, new()
{
protected DataAnnotationRelationalTestBase(TFixture fixture)
: base(fixture)
{
}

[ConditionalFact]
public virtual void Table_can_configure_TPT_with_Owned()
{
ExecuteWithStrategyInTransaction(
context =>
{
var model = context.Model;

var animalType = model.FindEntityType(typeof(Animal));
Assert.Equal("Animals", animalType.GetTableMappings().Single().Table.Name);

var petType = model.FindEntityType(typeof(Pet));
Assert.Equal("Pets", petType.GetTableMappings().Last().Table.Name);

var tagNavigation = petType.FindNavigation(nameof(Pet.Tag));
var ownership = tagNavigation.ForeignKey;
Assert.True(ownership.IsRequiredDependent);

var petTagType = ownership.DeclaringEntityType;
Assert.Equal("Pets", petTagType.GetTableMappings().Single().Table.Name);

var tagIdProperty = petTagType.FindProperty(nameof(PetTag.TagId));
Assert.False(tagIdProperty.IsNullable);
Assert.All(tagIdProperty.GetTableColumnMappings(), m => Assert.False(m.Column.IsNullable));

var catType = model.FindEntityType(typeof(Cat));
Assert.Equal("Cats", catType.GetTableMappings().Last().Table.Name);

var dogType = model.FindEntityType(typeof(Dog));
Assert.Equal("Dogs", dogType.GetTableMappings().Last().Table.Name);

context.Set<Cat>().Add(
new Cat { Key = 1, Species = "Felis catus", Tag = new PetTag { TagId = 2 } });

context.SaveChanges();
},
context =>
{
var cat = context.Set<Cat>().Single();
Assert.Equal("Felis catus", cat.Species);
Assert.Equal(2u, cat.Tag.TagId);
});
}

public abstract class DataAnnotationRelationalFixtureBase : DataAnnotationFixtureBase
{
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
base.OnModelCreating(modelBuilder, context);

modelBuilder.Entity<Animal>().ToTable("Animals").Property(x => x.Key).ValueGeneratedNever();
modelBuilder.Entity<Pet>().ToTable("Pets").HasOne(x => x.CatFriend);
modelBuilder.Entity<Cat>().ToTable("Cats");
modelBuilder.Entity<Dog>().ToTable("Dogs");
}
}

[Table("Animals")]
protected class Animal
{
[Key]
public int Key { get; set; }
public string Species { get; set; }
}

[Table("Pets")]
protected class Pet : Animal
{
public string Name { get; set; }

[Column("CatFriend_Id")]
[ForeignKey(nameof(CatFriend))]
public int? CatFriendId { get; set; }
public Cat CatFriend { get; set; }

[Required]
public PetTag Tag { get; set; }
}

[Table("Cats")]
protected sealed class Cat : Pet
{
public string EducationLevel { get; set; }
}

[Table("Dogs")]
protected sealed class Dog : Pet
{
public string FavoriteToy { get; set; }
}

[Owned]
protected sealed class PetTag
{
[Required]
public uint? TagId { get; set; }
}
}
}
3 changes: 3 additions & 0 deletions test/EFCore.Specification.Tests/DataAnnotationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ protected DbContext CreateContext()
protected virtual void ExecuteWithStrategyInTransaction(Action<DbContext> testOperation)
=> TestHelpers.ExecuteWithStrategyInTransaction(CreateContext, UseTransaction, testOperation);

protected virtual void ExecuteWithStrategyInTransaction(Action<DbContext> testOperation1, Action<DbContext> testOperation2)
=> TestHelpers.ExecuteWithStrategyInTransaction(CreateContext, UseTransaction, testOperation1, testOperation2);

protected virtual void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// ReSharper disable InconsistentNaming
namespace Microsoft.EntityFrameworkCore
{
public class DataAnnotationSqlServerTest : DataAnnotationTestBase<DataAnnotationSqlServerTest.DataAnnotationSqlServerFixture>
public class DataAnnotationSqlServerTest : DataAnnotationRelationalTestBase<DataAnnotationSqlServerTest.DataAnnotationSqlServerFixture>
{
// ReSharper disable once UnusedParameter.Local
public DataAnnotationSqlServerTest(DataAnnotationSqlServerFixture fixture, ITestOutputHelper testOutputHelper)
Expand Down Expand Up @@ -276,7 +276,7 @@ public override void TimestampAttribute_throws_if_value_in_database_changed()
private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

public class DataAnnotationSqlServerFixture : DataAnnotationFixtureBase
public class DataAnnotationSqlServerFixture : DataAnnotationRelationalFixtureBase
{
protected override ITestStoreFactory TestStoreFactory
=> SqlServerTestStoreFactory.Instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand All @@ -10,7 +9,7 @@

namespace Microsoft.EntityFrameworkCore
{
public class DataAnnotationSqliteTest : DataAnnotationTestBase<DataAnnotationSqliteTest.DataAnnotationSqliteFixture>
public class DataAnnotationSqliteTest : DataAnnotationRelationalTestBase<DataAnnotationSqliteTest.DataAnnotationSqliteFixture>
{
// ReSharper disable once UnusedParameter.Local
public DataAnnotationSqliteTest(DataAnnotationSqliteFixture fixture, ITestOutputHelper testOutputHelper)
Expand Down Expand Up @@ -165,12 +164,10 @@ public override void TimestampAttribute_throws_if_value_in_database_changed()
Assert.True(context.Model.FindEntityType(typeof(Two)).FindProperty("Timestamp").IsConcurrencyToken);
}

private static readonly string _eol = Environment.NewLine;

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

public class DataAnnotationSqliteFixture : DataAnnotationFixtureBase
public class DataAnnotationSqliteFixture : DataAnnotationRelationalFixtureBase
{
protected override ITestStoreFactory TestStoreFactory
=> SqliteTestStoreFactory.Instance;
Expand Down