Skip to content

Commit

Permalink
Don't set constructor binding by convention if already set
Browse files Browse the repository at this point in the history
Fixes #13891
  • Loading branch information
ajcvickers committed Jul 7, 2019
1 parent 321a236 commit c93c977
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
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

0 comments on commit c93c977

Please sign in to comment.