Skip to content

Commit

Permalink
Cosmos: Don't map collections as owned types
Browse files Browse the repository at this point in the history
Fixes #25749
Fixes #24684
  • Loading branch information
AndriySvyryd authored Sep 1, 2021
1 parent 0328ee8 commit 538dab2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public CosmosRelationshipDiscoveryConvention(ProviderConventionSetBuilderDepende
/// <param name="model"> The model. </param>
/// <returns> <see langword="true"/> if the given entity type should be owned. </returns>
public static bool ShouldBeOwnedType(Type targetType, IConventionModel model)
=> !targetType.IsGenericType || targetType.GetGenericTypeDefinition() != typeof(List<>);
=> !targetType.IsGenericType
|| targetType == typeof(Dictionary<string, object>)
|| targetType.GetInterface(typeof(IEnumerable<>).Name) == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override void Object_to_string_conversion()
base.Object_to_string_conversion();

AssertSql(
@"SELECT c[""TestSignedByte""], c[""TestByte""], c[""TestInt16""], c[""TestUnsignedInt16""], c[""TestInt32""], c[""TestUnsignedInt32""], c[""TestInt64""], c[""TestUnsignedInt64""], c[""TestSingle""], c[""TestDouble""], c[""TestDecimal""], c[""TestCharacter""], c[""TestDateTime""], c, c[""TestTimeSpan""]
@"SELECT c[""TestSignedByte""], c[""TestByte""], c[""TestInt16""], c[""TestUnsignedInt16""], c[""TestInt32""], c[""TestUnsignedInt32""], c[""TestInt64""], c[""TestUnsignedInt64""], c[""TestSingle""], c[""TestDouble""], c[""TestDecimal""], c[""TestCharacter""], c[""TestDateTime""], c[""TestDateTimeOffset""], c[""TestTimeSpan""]
FROM root c
WHERE ((c[""Discriminator""] = ""BuiltInDataTypes"") AND (c[""Id""] = 13))");
}
Expand Down Expand Up @@ -117,10 +117,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
shadowJObject.SetConfigurationSource(ConfigurationSource.Convention);
var nullableShadowJObject = (Property)modelBuilder.Entity<BuiltInNullableDataTypesShadow>().Property("__jObject").Metadata;
nullableShadowJObject.SetConfigurationSource(ConfigurationSource.Convention);

// Issue #24684
modelBuilder.Entity<BuiltInDataTypes>().Ignore(e => e.TestDateTimeOffset);
modelBuilder.Entity<BuiltInDataTypesShadow>().Ignore("TestDateTimeOffset");
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/EFCore.Tests/ModelBuilding/NonRelationshipTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,24 @@ protected class ThreeDee
public int[,,] Three { get; set; }
}

[ConditionalFact]
protected virtual void Throws_for_int_keyed_dictionary()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<IntDict>();

Assert.Equal(
CoreStrings.EntityRequiresKey(typeof(Dictionary<int, string>).ShortDisplayName()),
Assert.Throws<InvalidOperationException>(() => modelBuilder.FinalizeModel()).Message);
}

protected class IntDict
{
public int Id { get; set; }
public Dictionary<int, string> Notes { get; set; }
}

[ConditionalFact]
public virtual void Can_set_unicode_for_properties()
{
Expand Down

0 comments on commit 538dab2

Please sign in to comment.