Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Don't set constructor binding by convention if already set #16486

Merged
merged 1 commit into from
Jul 7, 2019
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 @@ -48,7 +48,8 @@ public virtual void ProcessModelFinalized(IConventionModelBuilder modelBuilder,
{
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
{
if (entityType.ClrType?.IsAbstract == false)
if (entityType.ClrType?.IsAbstract == false
&& entityType.Builder.CanSetAnnotation(CoreAnnotationNames.ConstructorBinding, null))
{
var maxServiceParams = 0;
var minPropertyParams = int.MaxValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,38 @@ public void Throws_if_two_constructors_with_same_number_of_parameters_could_be_u
() => GetBinding<BlogConflict>()).Message);
}

[ConditionalFact]
public void Does_not_throw_if_explicit_binding_has_been_set()
{
var constructorBinding = GetBinding<BlogConflict>(
e => e[CoreAnnotationNames.ConstructorBinding] = new ConstructorBinding(
typeof(BlogConflict).GetConstructor(
new[]
{
typeof(string),
typeof(int)
}),
new[]
{
new PropertyParameterBinding(e.FindProperty(nameof(Blog.Title))),
new PropertyParameterBinding(e.FindProperty(nameof(Blog.Id)))
}));

Assert.NotNull(constructorBinding);

var parameters = constructorBinding.Constructor.GetParameters();
var bindings = constructorBinding.ParameterBindings;

Assert.Equal(2, parameters.Length);
Assert.Equal(2, bindings.Count);

Assert.Equal("title", parameters[0].Name);
Assert.Equal("id", parameters[1].Name);

Assert.Equal("Title", bindings[0].ConsumedProperties.First().Name);
Assert.Equal("Id", bindings[1].ConsumedProperties.First().Name);
}

private class BlogConflict : Blog
{
public BlogConflict(string title, int id)
Expand Down Expand Up @@ -747,7 +779,7 @@ private class NoFieldRelated
public NoField NoField { get; set; }
}

private ConstructorBinding GetBinding<TEntity>()
private ConstructorBinding GetBinding<TEntity>(Action<IMutableEntityType> setBinding = null)
{
var entityType = ((IMutableModel)new Model()).AddEntityType(typeof(TEntity));
entityType.AddProperty(nameof(Blog.Id), typeof(int));
Expand All @@ -762,6 +794,8 @@ private ConstructorBinding GetBinding<TEntity>()
entityType.AddProperty("_FooBaar5", typeof(string));
entityType.AddProperty("m_FooBaar6", typeof(string));

setBinding?.Invoke(entityType);

var model = (Model)entityType.Model;
var context = new ConventionContext<IConventionModelBuilder>(model.ConventionDispatcher);

Expand Down