Skip to content

Commit

Permalink
Merge branch 'dotnet:main' into TrimStartSql
Browse files Browse the repository at this point in the history
  • Loading branch information
abdielcs authored May 16, 2024
2 parents f71bd4c + fa093d4 commit 610e500
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
2 changes: 0 additions & 2 deletions src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;

Expand Down
25 changes: 17 additions & 8 deletions src/EFCore/Internal/DbContextServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.Internal;

Expand Down Expand Up @@ -84,14 +85,22 @@ private IModel CreateModel(bool designTime)
}
}

return modelFromOptions == null
|| (designTime && modelFromOptions is not Metadata.Internal.Model)
? RuntimeFeature.IsDynamicCodeSupported
? dependencies.ModelSource.GetModel(_currentContext!.Context, dependencies, designTime)
: designTime
? throw new InvalidOperationException(CoreStrings.NativeAotDesignTimeModel)
: throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel)
: dependencies.ModelRuntimeInitializer.Initialize(modelFromOptions, designTime, dependencies.ValidationLogger);
if (modelFromOptions == null
|| (designTime && !(modelFromOptions is Model)))
{
return RuntimeFeature.IsDynamicCodeSupported
? dependencies.ModelSource.GetModel(_currentContext!.Context, dependencies, designTime)
: designTime
? throw new InvalidOperationException(CoreStrings.NativeAotDesignTimeModel)
: throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel);
}

var designTimeModel = dependencies.ModelRuntimeInitializer.Initialize(
modelFromOptions, designTime: modelFromOptions is Model, dependencies.ValidationLogger);

var runtimeModel = (IModel)designTimeModel.FindRuntimeAnnotationValue(CoreAnnotationNames.ReadOnlyModel)!;

return designTime ? designTimeModel : runtimeModel;
}
finally
{
Expand Down
25 changes: 23 additions & 2 deletions test/EFCore.Relational.Specification.Tests/F1RelationalFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,48 @@ protected override void BuildModelExternal(ModelBuilder modelBuilder)
modelBuilder.Entity<Gearbox>().ToTable("Gearboxes");
modelBuilder.Entity<Sponsor>().ToTable("Sponsors");

modelBuilder.Entity<FanTpt>().UseTptMappingStrategy();
modelBuilder.Entity<FanTpc>().UseTpcMappingStrategy();
modelBuilder.Entity<Fan>(
b =>
{
b.Property(e => e.Id).ValueGeneratedNever();
});

modelBuilder.Entity<FanTpt>(
b =>
{
b.UseTptMappingStrategy();
b.Property(e => e.Id).ValueGeneratedNever();
});

modelBuilder.Entity<FanTpc>(
b =>
{
b.UseTpcMappingStrategy();
b.Property(e => e.Id).ValueGeneratedNever();
});

modelBuilder.Entity<Circuit>(
b =>
{
b.ToTable("Circuits");
b.Property(e => e.Name).HasColumnName("Name");
b.Property(e => e.Id).ValueGeneratedNever();
});

modelBuilder.Entity<City>(
b =>
{
b.ToTable("Circuits");
b.Property(e => e.Name).HasColumnName("Name");
b.Property(e => e.Id).ValueGeneratedNever();
});

modelBuilder.Entity<CircuitTpt>(
b =>
{
b.UseTptMappingStrategy();
b.Property(e => e.Name).HasColumnName("Name");
b.Property(e => e.Id).ValueGeneratedNever();
});

modelBuilder.Entity<StreetCircuitTpt>(
Expand All @@ -70,6 +90,7 @@ protected override void BuildModelExternal(ModelBuilder modelBuilder)
{
b.UseTpcMappingStrategy();
b.Property(e => e.Name).HasColumnName("Name");
b.Property(e => e.Id).ValueGeneratedNever();
});

modelBuilder.Entity<StreetCircuitTpc>(
Expand Down
25 changes: 25 additions & 0 deletions test/EFCore.Relational.Tests/Metadata/RelationalModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ public void GetRelationalModel_throws_if_convention_has_not_run()
() => ((IModel)modelBuilder.Model).GetRelationalModel()).Message);
}

[ConditionalFact]
public void Both_design_and_runtime_RelationalModels_are_built_for_external_model()
{
var modelBuilder = CreateConventionModelBuilder();
modelBuilder.Ignore<OrderDetails>();
modelBuilder.Ignore<DateDetails>();
modelBuilder.Ignore<Customer>();
modelBuilder.Entity<Order>().ToTable(tb => tb.HasCheckConstraint("OrderCK", "[Id] > 0"));

var options = FakeRelationalTestHelpers.Instance.CreateOptions((IModel)modelBuilder.Model);
using var context = new DbContext(options);

var designTimeModel = context.GetService<IDesignTimeModel>().Model;
var runtimeModel = context.Model;
Assert.NotSame(designTimeModel.FindRuntimeAnnotationValue(RelationalAnnotationNames.RelationalModelFactory),
runtimeModel.FindRuntimeAnnotationValue(RelationalAnnotationNames.RelationalModelFactory));

var designTimeRelationalModel = designTimeModel.GetRelationalModel();
var runtimeRelationalModel = runtimeModel.GetRelationalModel();
Assert.NotSame(designTimeRelationalModel, runtimeRelationalModel);

Assert.Single(designTimeRelationalModel.Tables.Single().CheckConstraints);
Assert.Empty(((Table)runtimeRelationalModel.Tables.Single()).CheckConstraints);
}

[ConditionalTheory]
[InlineData(true, Mapping.TPH)]
[InlineData(true, Mapping.TPT)]
Expand Down
7 changes: 6 additions & 1 deletion test/EFCore.Specification.Tests/TestUtilities/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.DependencyInjection.Extensions;

Expand Down Expand Up @@ -430,7 +431,11 @@ public override IModel FinalizeModel()
=> FinalizeModel(designTime: false);

public IModel FinalizeModel(bool designTime = false, bool skipValidation = false)
=> _modelRuntimeInitializer.Initialize((IModel)Model, designTime, skipValidation ? null : _validationLogger);
{
var designTimeModel = _modelRuntimeInitializer.Initialize((IModel)Model, designTime: true, skipValidation ? null : _validationLogger);
var runtimeModel = (IModel)designTimeModel.FindRuntimeAnnotationValue(CoreAnnotationNames.ReadOnlyModel)!;
return designTime ? designTimeModel : runtimeModel;
}
}

public class TestModelConfigurationBuilder(ConventionSet conventionSet, IServiceProvider serviceProvider)
Expand Down

0 comments on commit 610e500

Please sign in to comment.