Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,15 @@ private void RegisterPrivateAccessors(
Dictionary<Type, HashSet<MemberInfo>> unsafeAccessorTypes,
ref Dictionary<MemberInfo, QualifiedName>? memberAccessReplacements)
{
var member = property.GetMemberInfo(forMaterialization, forSet);
if (!property.TryGetMemberInfo(forMaterialization, forSet, out var member, out var error))
{
throw new InvalidOperationException(error);
}

if (member == null)
{
return null;
}
switch (member)
{
case FieldInfo field:
Expand Down Expand Up @@ -1799,6 +1807,7 @@ private void RegisterPrivateAccessors(
}

memberAccessReplacements ??= [];

var methodName = LinqToCSharpSyntaxTranslator.GetUnsafeAccessorName(member);

Comment thread
AndriySvyryd marked this conversation as resolved.
var declaringType = member.DeclaringType!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ protected override BuildSource AddReferences(BuildSource build, [CallerFilePath]
IEnumerable<ScaffoldedFile>? additionalSourceFiles = null,
Action<Assembly>? assertAssembly = null,
string? expectedExceptionMessage = null,
bool skipValidation = false,
[CallerMemberName] string testName = "")
where TContext : class
=> base.Test(
Expand All @@ -706,5 +707,6 @@ protected override BuildSource AddReferences(BuildSource build, [CallerFilePath]
additionalSourceFiles,
assertAssembly,
expectedExceptionMessage,
skipValidation,
testName);
}
5 changes: 3 additions & 2 deletions test/EFCore.Specification.Tests/NonSharedModelTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ protected virtual ContextFactory<TContext> CreateContextFactory<TContext>(
Func<string, bool>? shouldLogCategory = null,
Func<TestStore>? createTestStore = null,
bool usePooling = true,
bool useServiceProvider = true)
bool useServiceProvider = true,
bool skipValidation = false)
where TContext : DbContext
{
if (createTestStore != null)
Expand All @@ -110,7 +111,7 @@ protected virtual ContextFactory<TContext> CreateContextFactory<TContext>(

if (onModelCreating != null)
{
services = services.AddSingleton(TestModelSource.GetFactory(onModelCreating, configureConventions));
services = services.AddSingleton(TestModelSource.GetFactory(onModelCreating, configureConventions, skipValidation));
}

addServices?.Invoke(services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,21 @@ public virtual Task ComplexTypes()
},
options: new CompiledModelCodeGenerationOptions { UseNullableReferenceTypes = true, ForNativeAot = true });

[ConditionalFact]
public virtual Task Throws_for_Backing_Field_Not_Found()
=> Test(
modelBuilder =>
{
modelBuilder.Entity<EntityWithNoBackingFieldScalar>(eb =>
{
eb.Property(e => e.Computed)
.UsePropertyAccessMode(PropertyAccessMode.FieldDuringConstruction);
});
},
options: new CompiledModelCodeGenerationOptions { ForNativeAot = true },
expectedExceptionMessage: CoreStrings.NoFieldOrSetter("Computed", "EntityWithNoBackingFieldScalar"),
skipValidation: true);

protected virtual void BuildComplexTypesModel(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PrincipalBase>(eb =>
Expand Down Expand Up @@ -1790,6 +1805,12 @@ public class Data
public byte[]? Blob { get; set; }
}

public class EntityWithNoBackingFieldScalar
{
public int Id { get; set; }
public int Computed => 1;
}

public class PrincipalBase : AbstractBase
{
public new long? Id { get; set; }
Expand Down Expand Up @@ -2007,6 +2028,7 @@ protected virtual Task Test(
IEnumerable<ScaffoldedFile>? additionalSourceFiles = null,
Action<Assembly>? assertAssembly = null,
string? expectedExceptionMessage = null,
bool skipValidation = false,
[CallerMemberName] string testName = "")
=> Test<DbContext>(
onModelCreating,
Expand All @@ -2019,6 +2041,7 @@ protected virtual Task Test(
additionalSourceFiles,
assertAssembly,
expectedExceptionMessage,
skipValidation,
testName);

protected virtual async Task<(TContext?, IModel?)> Test<TContext>(
Expand All @@ -2032,6 +2055,7 @@ protected virtual Task Test(
IEnumerable<ScaffoldedFile>? additionalSourceFiles = null,
Action<Assembly>? assertAssembly = null,
string? expectedExceptionMessage = null,
bool skipValidation = false,
[CallerMemberName] string testName = "")
where TContext : DbContext
{
Expand All @@ -2044,7 +2068,8 @@ protected virtual Task Test(
onModelCreating?.Invoke(modelBuilder);
},
onConfiguring,
addServices);
addServices,
skipValidation: skipValidation);
using var context = contextFactory.CreateContext();
var model = context.GetService<IDesignTimeModel>().Model;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ public class TestModelSource : ModelSource
{
private readonly Action<ModelConfigurationBuilder>? _configureConventions;
private readonly Action<ModelBuilder, DbContext> _onModelCreating;
private readonly bool _skipValidation;

private TestModelSource(
Action<ModelConfigurationBuilder>? configureConventions,
Action<ModelBuilder, DbContext> onModelCreating,
ModelSourceDependencies dependencies)
ModelSourceDependencies dependencies,
bool skipValidation = false)
: base(dependencies)
{
_configureConventions = configureConventions;
_onModelCreating = onModelCreating;
_skipValidation = skipValidation;
}

public override IModel CreateModel(
DbContext context,
ModelCreationDependencies modelCreationDependencies,
bool designTime)
{
var model = CreateModel(context, modelCreationDependencies.ConventionSetBuilder, modelCreationDependencies.ModelDependencies);
return modelCreationDependencies.ModelRuntimeInitializer.Initialize(
model, designTime, _skipValidation ? null : modelCreationDependencies.ValidationLogger);
}

protected override IModel CreateModel(
Expand All @@ -41,17 +54,21 @@ protected override IModel CreateModel(

public static Func<IServiceProvider, IModelSource> GetFactory(
Action<ModelBuilder> onModelCreating,
Action<ModelConfigurationBuilder>? configureConventions = null)
Action<ModelConfigurationBuilder>? configureConventions = null,
bool skipValidation = false)
=> p => new TestModelSource(
configureConventions,
(mb, c) => onModelCreating(mb),
p.GetRequiredService<ModelSourceDependencies>());
p.GetRequiredService<ModelSourceDependencies>(),
skipValidation);

public static Func<IServiceProvider, IModelSource> GetFactory(
Action<ModelBuilder, DbContext> onModelCreating,
Action<ModelConfigurationBuilder>? configureConventions = null)
Action<ModelConfigurationBuilder>? configureConventions = null,
bool skipValidation = false)
=> p => new TestModelSource(
configureConventions,
onModelCreating,
p.GetRequiredService<ModelSourceDependencies>());
p.GetRequiredService<ModelSourceDependencies>(),
skipValidation);
}
Loading