System.InvalidOperationException: The constructors '(int)' and '(string)' have the same number of parameters, and can both be used by EF Core

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

System.InvalidOperationException: The constructors '(int)' and '(string)' have the same number of parameters, and can both be used by EF Core

问题

我正在使用Entity Framework Core 7.0.7的这个类:

public class Collaborator : Entity
{
    public Collaborator(int productId)
    {
        ProductId = productId;
    }

    public Collaborator(string userId)
    {
        UserId = userId;
    }

    public string UserId { get; set; }
    public User User { get; set; }

    public int ProductId { get; set; }
    public Product Product { get; set; }
}

我遇到了以下错误:

System.InvalidOperationException: '构造函数 '(int)' 和 '(string)' 具有相同数量的参数,并且都可以被Entity Framework使用。必须在 'OnModelCreating' 中配置要使用的构造函数。'

我需要在 OnModelCreating 中配置构造函数吗,还是可以用另一种方式解决?

英文:

I'm using this class with Entity Framework Core 7.0.7:

public class Collaborator : Entity
{
    public Collaborator(int productId)
    {
        ProductId = productId;
    }

    public Collaborator(string userId)
    {
        UserId = userId;
    }

    public string UserId { get; set; }
    public User User { get; set; }

    public int ProductId { get; set; }
    public Product Product { get; set; }
}

I get this error:

> System.InvalidOperationException: 'The constructors '(int)' and '(string)' have the same number of parameters, and can both be used by Entity Framework. The constructor to be used must be configured in 'OnModelCreating'.'

Do I need to configure the constructor in OnModelCreating or can it be solved in another way?

答案1

得分: 1

使用私有的无参数构造函数解决了这个问题,如下所示:

private Collaborator()
{
}
英文:

Solved it with a private parameterless constructor like this:

private Collaborator() 
{ 
}

huangapple
  • 本文由 发表于 2023年6月30日 03:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584126.html
匿名

发表评论

匿名网友

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

确定