英文:
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()
{
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论