如何解决错误ApplicationRole“不包含采用参数1. [DbInitialize]的构造函数?
How solve error ApplicationRole "does not contain a constructor that takes arguments 1. [DbInitialize]?
我创建了 class ApplicationRole 并继承自 IdentityRole
using Microsoft.AspNetCore.Identity;
namespace ProjDAL.Entities
{
public class ApplicationRole : IdentityRole
{
}
}
当我尝试添加新角色时出现错误:
if (await _roleManager.FindByNameAsync("Quality Manager") == null)
{
await _roleManager.CreateAsync(new ApplicationRole("Quality Manager"));
}
'ApplicationRole "不包含接受参数1的构造函数。[DbInitialize]
更新:
我已经实现了构造函数:
public class ApplicationRole : IdentityRole
{
public ApplicationRole(string roleName) : base(roleName)
{
}
}
但现在出现错误:
System.InvalidOperationException: No suitable constructor found for entity
type 'ApplicationRole'. The following constructors had parameters that could
not be bound to properties of the entity type: cannot bind 'roleName'
in ApplicationRole(string roleName).
简答: 如下更改代码
public class ApplicationRole : IdentityRole<string>
{
public ApplicationRole() : base()
{
}
public ApplicationRole(string roleName) : base(roleName)
{
}
}
长版:
'ApplicationRole "does not contain a constructor that takes arguments 1. [DbInitialize]`
出现第一个错误是因为您试图通过
创建新角色
new ApplicationRole("Quality Manager")
但是,没有接受单个字符串作为参数的构造函数:
public class ApplicationRole : IdentityRole
{
}
所以它抱怨
does not contain a constructor that takes arguments 1. [DbInitialize]
注意当没有显式构造函数时,C# 将为您create one by default。
但是,如果您添加如下构造函数:
public class ApplicationRole : IdentityRole
{
public ApplicationRole(string roleName) : base(roleName)
{
}
}
只有一个构造函数接受 string
作为 roleName
。请注意,这意味着 没有不带参数的构造函数 。由于此构造函数(不带参数)由 Identity
在内部使用,因此它抱怨 No suitable constructor found for entity type 'ApplicationRole'
.
因此,如果您想通过以下方式创建 ApplicationRole
:
new ApplicationRole("Quality Manager")
您需要同时创建 ApplicationRole()
和 ApplicationRole(string roleName)
构造函数。
我创建了 class ApplicationRole 并继承自 IdentityRole
using Microsoft.AspNetCore.Identity;
namespace ProjDAL.Entities
{
public class ApplicationRole : IdentityRole
{
}
}
当我尝试添加新角色时出现错误:
if (await _roleManager.FindByNameAsync("Quality Manager") == null)
{
await _roleManager.CreateAsync(new ApplicationRole("Quality Manager"));
}
'ApplicationRole "不包含接受参数1的构造函数。[DbInitialize]
更新:
我已经实现了构造函数:
public class ApplicationRole : IdentityRole
{
public ApplicationRole(string roleName) : base(roleName)
{
}
}
但现在出现错误:
System.InvalidOperationException: No suitable constructor found for entity
type 'ApplicationRole'. The following constructors had parameters that could
not be bound to properties of the entity type: cannot bind 'roleName'
in ApplicationRole(string roleName).
简答: 如下更改代码
public class ApplicationRole : IdentityRole<string>
{
public ApplicationRole() : base()
{
}
public ApplicationRole(string roleName) : base(roleName)
{
}
}
长版:
'ApplicationRole "does not contain a constructor that takes arguments 1. [DbInitialize]`
出现第一个错误是因为您试图通过
创建新角色new ApplicationRole("Quality Manager")
但是,没有接受单个字符串作为参数的构造函数:
public class ApplicationRole : IdentityRole
{
}
所以它抱怨
does not contain a constructor that takes arguments 1. [DbInitialize]
注意当没有显式构造函数时,C# 将为您create one by default。
但是,如果您添加如下构造函数:
public class ApplicationRole : IdentityRole
{
public ApplicationRole(string roleName) : base(roleName)
{
}
}
只有一个构造函数接受 string
作为 roleName
。请注意,这意味着 没有不带参数的构造函数 。由于此构造函数(不带参数)由 Identity
在内部使用,因此它抱怨 No suitable constructor found for entity type 'ApplicationRole'
.
因此,如果您想通过以下方式创建 ApplicationRole
:
new ApplicationRole("Quality Manager")
您需要同时创建 ApplicationRole()
和 ApplicationRole(string roleName)
构造函数。