Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/EFCore/Metadata/Internal/ForeignKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,13 @@ public static bool AreCompatible(

if (!ArePropertyTypesCompatible(principalProperties, dependentProperties))
{
if (principalEntityType.Model is Model model
&& ReferenceEquals(model, dependentEntityType.Model)
&& model.IsInModelSnapshot)
{
return true;
}

if (shouldThrow)
{
throw new InvalidOperationException(
Expand Down
14 changes: 14 additions & 0 deletions src/EFCore/Metadata/Internal/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ public virtual ModelDependencies? ScopedModelDependencies
set => _scopedModelDependencies = value;
}

/// <summary>
/// Gets a value indicating whether this model originated from a migration snapshot.
/// </summary>
/// <remarks>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </remarks>
public virtual bool IsInModelSnapshot
=> ScopedModelDependencies == null
&& _modelFinalizedConventions is { Count: 0 }
&& FindAnnotation(CoreAnnotationNames.ProductVersion) != null;
Comment on lines +105 to +117

/// <summary>
/// Indicates whether the model is read-only.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,54 @@ protected override void BuildModel(ModelBuilder modelBuilder)
Assert.Equal(2, snapshot.Model.GetEntityTypes().Count());
}

[Fact]
public void Snapshot_with_mismatched_key_and_foreign_key_property_types_is_usable()
{
const string snapshotCode =
"""
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable disable

namespace RootNamespace;

partial class Snapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "10.0.0");

modelBuilder.Entity("Dependent", b =>
{
b.Property<long>("Id")
.HasColumnType("bigint");

b.HasKey("Id");

b.HasOne("Principal")
.WithMany()
.HasForeignKey("Id");
});

modelBuilder.Entity("Principal", b =>
{
b.Property<short>("Id")
.HasColumnType("smallint");

b.HasKey("Id");
});
}
}

""";

var snapshotModel = BuildModelFromSnapshotSource(snapshotCode);

Assert.Single(snapshotModel.FindEntityType("Dependent")!.GetForeignKeys());
}

[Fact]
public void Snapshot_with_migration_id()
{
Expand Down
12 changes: 12 additions & 0 deletions test/EFCore.Tests/Infrastructure/ModelValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,18 @@ public virtual void Warns_on_double_uniquified_shadow_key_due_to_wrong_type()
modelBuilder);
}

[Fact]
public virtual void Passes_on_foreign_key_with_matching_model_type_and_mismatched_provider_type()
{
var modelBuilder = CreateConventionModelBuilder();

modelBuilder.Entity<A>().HasOne<B>().WithMany().HasForeignKey(a => a.P0).HasPrincipalKey(b => b.Id);
modelBuilder.Entity<A>().Property(a => a.P0).HasConversion<long>();
modelBuilder.Entity<B>().Property(b => b.Id).HasConversion<short>();

Validate(modelBuilder);
}

[Fact]
public virtual void Detects_shadow_key_referenced_by_foreign_key_by_convention()
{
Expand Down
Loading