[英]Application Role Name Cannot Be Null Or Empty
So I am trying to create a new role in my MVC5 application. 因此,我试图在我的MVC5应用程序中创建一个新角色。 Every time I get an exception saying:
每次出现异常情况时,都会说:
Name cannot be null or empty
名称不能为null或为空
Model: 模型:
public class ApplicationRole : IdentityRole
{
public new string Id { get; set; }
[Display(Name = "Name")]
public new string Name { get; set; }
[Display(Name = "Description")]
[StringLength(100, MinimumLength = 5)]
public string Description { get; set; }
}
Controller: 控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(ApplicationRole model)
{
try
{
if (ModelState.IsValid)
{
var role = new ApplicationRole()
{
Name = model.Name,
Description = model.Description
};
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
var result = await roleManager.CreateAsync(role);
if (result.Succeeded)
{
return RedirectToAction("Index", "Roles", model);
}
else
{
AddErrors(result);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
// If we got this far, something failed, redisplay form
return View(model);
}
Execution stops on the following line: 执行在以下行停止:
var result = await roleManager.CreateAsync(role);
View: 视图:
@model IEnumerable<User_Manager_Interface.Models.ApplicationRole>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.ActionLink("Create New", "Create", "Roles", new object { }, new { @class = "stdbtn" })
<div class="contenttitle radiusbottom0">
<h2 class="table"><span>Roles</span></h2>
</div>
<table cellpadding="0" cellspacing="0" border="0" class="stdtable" id="dyntable">
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</tbody>
</table>
What I don't get is that role does get the fields I pass in my view but the error persists. 我没有得到的是该角色确实获得了我认为通过的字段,但错误仍然存在。 Any suggestions on how I can get around this?
关于如何解决这个问题有什么建议吗? Hopefully I have provided enough to reproduce the error.
希望我提供了足够的信息来重现该错误。
UPDATE to my scenario: 更新我的方案:
IdentityRole has its own Id and Name properties and if I declare them within the ApplicationRole model and decorating them with the new keyword it will obviously hide the inherited member. IdentityRole具有自己的Id和Name属性,如果我在ApplicationRole模型中声明它们并用new关键字修饰它们,则它将显然隐藏继承的成员。 I was aware of this when I posted the question.
发布问题时,我意识到了这一点。
If I remove the two properties altogether, then the application throws an exception at Runtime detailed: 如果我完全删除了这两个属性,那么应用程序将在运行时详细抛出异常:
Mapping and metadata information could not be found for EntityType 'User_Manager_Interface.Models.ApplicationRole
找不到EntityType'User_Manager_Interface.Models.ApplicationRole的映射和元数据信息
Going back to point 1. above, the reason I went with that implementation over 2. is because with 1. , the application does not terminate but instead throws a model state error (as described in the question title) about Name
property being null. 回到上面的第1点,我在2.上实现该实现的原因是因为在1.下 ,应用程序不会终止,而是引发有关
Name
属性为null的模型状态错误(如问题标题所述)。
I tried to examine the stack trace for more details about the exception but I couldn't gather more information other than what I already know: 我试图检查堆栈跟踪以获取有关该异常的更多详细信息,但是除了我已经知道的以外,我无法收集更多信息:
e.StackTrace "
e.StackTrace“
at System.Data.Entity.Core.Objects.ObjectContext.GetTypeUsage(Type entityCLRType)\\r\\n在System.Data.Entity.Core.Objects.ObjectContext.GetTypeUsage(TypeEntityCLRType)\\ r \\ n
at System.Data.Entity.Core.Objects.ObjectContext.ValidateEntitySet(EntitySet entitySet, Type entityType)\\r\\n在System.Data.Entity.Core.Objects.ObjectContext.ValidateEntitySet(EntitySet实体集,类型实体类型)\\ r \\ n
at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation)\\r\\n在System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(布尔doAttach,字符串entitySetName,IEntityWrapper包装的实体,EntityEntry existEntry,EntitySet&entitySet,Boolean&isNoOperation)\\ r \\ n
at System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity)\\r\\n在System.Data.Entity.Core.Objects.ObjectContext.AddObject(String实体集名称,对象实体)\\ r \\ n
at System.Data.Entity.Internal.Linq.InternalSet1.<>c__DisplayClassd.<Add>b__c()\\r\\n
at System.Data.Entity.Internal.Linq.InternalSet在System.Data.Entity.Internal.Linq.InternalSet
1.<>c__DisplayClassd.<Add>b__c()\\r\\n
at System.Data.Entity.Internal.Linq.InternalSet1.<>c__DisplayClassd.<Add>b__c()\\r\\n
at System.Data.Entity.Internal.Linq.InternalSet1.<>c__DisplayClassd.<Add>b__c()\\r\\n
1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)\\r\\n
at System.Data.Entity.Internal.Linq.InternalSet1.<>c__DisplayClassd.<Add>b__c()\\r\\n
1.ActOnSet(动作操作,EntityState newState,对象实体,字符串methodName)\\ r \\ n
at System.Data.Entity.Internal.Linq.InternalSet
at System.Data.Entity.Internal.Linq.InternalSet1.Add(Object entity)\\r\\n
at System.Data.Entity.DbSet在System.Data.Entity.Internal.Linq.InternalSet
1.Add(Object entity)\\r\\n
at System.Data.Entity.DbSet1.Add(Object entity)\\r\\n
at System.Data.Entity.DbSet1.Add(Object entity)\\r\\n
1.Add(TEntity entity)\\r\\n
at System.Data.Entity.DbSet1.Add(Object entity)\\r\\n
1.Add(TEntity实体)\\ r \\ n
at System.Data.Entity.DbSet
at Microsoft.AspNet.Identity.EntityFramework.RoleStore`3.d__2.MoveNext()\\r\\n---在Microsoft.AspNet.Identity.EntityFramework.RoleStore`3.d__2.MoveNext()\\ r \\ n ---
End of stack trace from previous location where exception was thrown ---\\r\\n
从上一个引发异常的位置开始的堆栈跟踪结束--- \\ r \\ n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\\ r \\ n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\\ r \\ n
at Microsoft.AspNet.Identity.RoleManager2.<CreateAsync>d__0.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter在Microsoft.AspNet.Identity.RoleManager
2.<CreateAsync>d__0.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter2.<CreateAsync>d__0.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter2.<CreateAsync>d__0.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n
1.GetResult()\\r\\n at User_Manager_Interface.Controllers.RolesController.d__5.MoveNext() in C:\\Users\\Linda\\UserManager\\FSKUserManager\\FSK_UserManager_Web\\Controllers\\RolesController.cs:line 104"
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter2.<CreateAsync>d__0.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n
1.GetResult()\\ r \\ n位于C:\\ Users \\ Linda \\ UserManager \\ FSKUserManager \\ FSK_UserManager_Web \\ Controllers \\ RolesController.cs:第104行中的User_Manager_Interface.Controllers.RolesController.d__5.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter
I think it has to do with the new
keyword in property declaration. 我认为这与属性声明中的
new
关键字有关。 If IdentityRole
has already these properties remove the following properties from your ApplicationRole
model 如果
IdentityRole
已经具有这些属性,请从ApplicationRole
模型中删除以下属性
public new string Id { get; set; }
[Display(Name = "Name")]
public new string Name { get; set; }
UPDATE : In your ApplicationRole
class, by declaring the Id
and Name
properties with the new
keyword, you are actually hide the base class ( IdentityRole
) properties. 更新 :在
ApplicationRole
类中,通过使用new
关键字声明Id
和Name
属性,实际上是在隐藏基类( IdentityRole
)属性。 As a result every time you are trying to save a new record these properties are null and you get the corresponding error. 如此一来,每次您尝试保存新记录时,这些属性将为null,并且会出现相应的错误。
Also change this line from 同时从
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
To 至
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(db));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.