[英]Application Role Name Cannot Be Null Or Empty
因此,我試圖在我的MVC5應用程序中創建一個新角色。 每次出現異常情況時,都會說:
名稱不能為null或為空
模型:
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; }
}
控制器:
[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);
}
執行在以下行停止:
var result = await roleManager.CreateAsync(role);
視圖:
@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>
我沒有得到的是該角色確實獲得了我認為通過的字段,但錯誤仍然存在。 關於如何解決這個問題有什么建議嗎? 希望我提供了足夠的信息來重現該錯誤。
更新我的方案:
IdentityRole具有自己的Id和Name屬性,如果我在ApplicationRole模型中聲明它們並用new關鍵字修飾它們,則它將顯然隱藏繼承的成員。 發布問題時,我意識到了這一點。
如果我完全刪除了這兩個屬性,那么應用程序將在運行時詳細拋出異常:
找不到EntityType'User_Manager_Interface.Models.ApplicationRole的映射和元數據信息
回到上面的第1點,我在2.上實現該實現的原因是因為在1.下 ,應用程序不會終止,而是引發有關Name
屬性為null的模型狀態錯誤(如問題標題所述)。
我試圖檢查堆棧跟蹤以獲取有關該異常的更多詳細信息,但是除了我已經知道的以外,我無法收集更多信息:
e.StackTrace“
在System.Data.Entity.Core.Objects.ObjectContext.GetTypeUsage(TypeEntityCLRType)\\ r \\ n
在System.Data.Entity.Core.Objects.ObjectContext.ValidateEntitySet(EntitySet實體集,類型實體類型)\\ r \\ n
在System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(布爾doAttach,字符串entitySetName,IEntityWrapper包裝的實體,EntityEntry existEntry,EntitySet&entitySet,Boolean&isNoOperation)\\ r \\ n
在System.Data.Entity.Core.Objects.ObjectContext.AddObject(String實體集名稱,對象實體)\\ r \\ n
在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
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
在System.Data.Entity.Internal.Linq.InternalSet1.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實體)\\ r \\ n
at System.Data.Entity.DbSet
在Microsoft.AspNet.Identity.EntityFramework.RoleStore`3.d__2.MoveNext()\\ r \\ n ---從上一個引發異常的位置開始的堆棧跟蹤結束--- \\ r \\ n
在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)\\ r \\ n
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\\ r \\ n
在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.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位於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
我認為這與屬性聲明中的new
關鍵字有關。 如果IdentityRole
已經具有這些屬性,請從ApplicationRole
模型中刪除以下屬性
public new string Id { get; set; }
[Display(Name = "Name")]
public new string Name { get; set; }
更新 :在ApplicationRole
類中,通過使用new
關鍵字聲明Id
和Name
屬性,實際上是在隱藏基類( IdentityRole
)屬性。 如此一來,每次您嘗試保存新記錄時,這些屬性將為null,並且會出現相應的錯誤。
同時從
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
至
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(db));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.