EF Core 如何确定使用哪个带参数的实体构造函数?

huangapple go评论61阅读模式
英文:

How does EF Core determine which parametrized entity constructor to use?

问题

I use the feature in EF Core 7.0 that allows using entities with parameterized constructors, described here.

However, I don't understand some behavior of EF Core when it comes to selecting which constructor to use.

For instance, I have this entity, with a readonly Id and thus a creation constructor, which generates the id and a reconstitution constructor with the Id as a parameter.

public class Poll
{
    public Guid Id { get; }
    public string Name { get; set; }
    public DateTime ExpirationDate { get; set; }
    public List<DateTime> Dates { get; set; } = new List<DateTime>();

    public Poll(string name, List<DateTime> dates) : this(
        Guid.NewGuid(),
        name,
        new List<Vote>(),
        dates,
        DateTime.UtcNow.AddMonths(2)
        )
    {
    }

    public Poll(Guid id, string name, List<DateTime> dates, DateTime expirationDate)
    {
        Name = name;
        Id = id;
        Dates = dates;
        ExpirationDate = expirationDate;
    }
}

with the following model binding :

modelBuilder.Entity<Poll>(entity =>
{
    entity.HasKey(poll => poll.Id);
    entity
        .Property(poll => poll.Id)
        .ValueGeneratedNever();
    entity.Property(poll => poll.Name);
    entity.Property(poll => poll.Dates);
    entity.Property(poll => poll.ExpirationDate);
});

When querying this entity, EF Core will call the first (creation) constructor whereas, I would like (and would find it logical, according to the docs) that it would rather use the second (reconstitution) constructor.
How come it's not using the constructor with a parameter for each mapped property, as per the docs :

However, if EF Core finds a parameterized constructor with parameter names and types that match those of mapped properties, then it will instead call the parameterized constructor with values for those properties and will not set each property explicitly.

EDIT: Experimented by removing the first constructor, and it threw an exception saying that the votes parameter could not be bound to a property. It worked when removing it. However, when adding back my first constructor, EF still goes back to using it instead of the other, so the question still stands.

英文:

I use the feature in EF Core 7.0 that allow using entities with parametrized constructors, described here.

However, I don't understand some behaviour of EF Core when it comes to selecting which constructor to use.

For instance I have this entity, with readonly Id, and thus a creation constructor, which generates the id and a reconstitution constructor with the Id as parameter.

public class Poll
{
    public Guid Id { get; }
    public string Name {get;set;}
    public DateTime ExpirationDate { get; set; }
    public List&lt;Vote&gt; Votes { get; set; }
    public List&lt;DateTime&gt; dates {get;set;} = new List&lt;DateTime&gt;();

    public Poll(string name, List&lt;DateTime&gt; dates) : this(
        Guid.NewGuid(), 
        name,
        new List&lt;Vote&gt;(), 
        dates, 
        DateTime.UtcNow.AddMonths(2)
        )
    {
    }

    public Poll(Guid id, string name, List&lt;DateTime&gt; dates, DateTime expirationDate)
    {
        Name = name;
        Id = id;
        Dates = dates;
        ExpirationDate = expirationDate;
    }





}

with the following model binding :

       modelBuilder.Entity&lt;Poll&gt;(entity =&gt;
        {
            entity.HasKey(poll =&gt; poll.Id);
            entity
                .Property(poll =&gt; poll.Id)
                .ValueGeneratedNever();
            entity.Property(poll =&gt; poll.Name);
            entity.Property(poll =&gt; poll.Dates);
            entity.Property(poll =&gt; poll.ExpirationDate);
        });

When querying this entity, EF Core will call the first (creation) constructor whereas, I would like (and would find it logical, according to the docs) that it would rather use the second (reconstitution) constructor.
How come it's not using the constructor with a parameter for each mapped property, as per the docs :

> However, if EF Core finds a parameterized constructor with parameter names and types that match those of mapped properties, then it will instead call the parameterized constructor with values for those properties and will not set each property explicitly.

EDIT : Experimented by removing the first constructor and it threw an exception saying that the votes parameter could not be bound to a property. It worked when removing it. However when adding back my first constructor, EF still goes back to using it instead of the other, so the question still stands.

答案1

得分: 3

GitHub上的源代码显示,使用最少数量的标量参数的构造函数 - 您没有服务,只有标量属性参数

ConstructorBindingFactory类的源代码中,这涉及到实体构造函数的选择:

> // 尝试找到具有最多服务属性的构造函数
> // 接着是最少标量属性参数

源代码中的单元测试演示了这种行为。
在下面的示例中,将选择具有2个参数的构造函数。

public void Binds_to_least_parameters_if_no_services()
{
    var constructorBinding = GetBinding&lt;BlogSeveral&gt;();

    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 BlogSeveral : Blog
{
    public BlogSeveral(string title, int id)
    { }

    public BlogSeveral(string title, Guid? shadow, int id)
    { }

    public BlogSeveral(string title, Guid? shadow, bool dummy, int id)
    { }
}

关于构造函数中的List&lt;Votes&gt; votes参数的错误,您之所以会遇到这个错误是因为EF Core无法使用构造函数设置导航属性,正如您链接到的文档中所提到的。

英文:

The source code on GitHub shows that the constructor with the least amount of scalar parameters gets used - you don't have services, only scalar property parameters.

From the source code of the ConstructorBindingFactory class, involved in the entity constructor selection:

> // Trying to find the constructor with the most service properties
> // followed by the least scalar property parameters

A unit test in the source code demonstrates this behavior.
In below example, the constructor with 2 parameters will be chosen.

public void Binds_to_least_parameters_if_no_services()
{
    var constructorBinding = GetBinding&lt;BlogSeveral&gt;();

    Assert.NotNull(constructorBinding);

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

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

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

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

private class BlogSeveral : Blog
{
    public BlogSeveral(string title, int id)
    { }

    public BlogSeveral(string title, Guid? shadow, int id)
    { }

    public BlogSeveral(string title, Guid? shadow, bool dummy, int id)
    { }
}

About that error on the constructor with the List&lt;Votes&gt; votes argument, you get that because EF Core cannot set navigation properties using a constructor, as mentioned in the documentation you linked to.

huangapple
  • 本文由 发表于 2023年5月14日 23:51:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定