所以我想在我的MVC5应用程序中创建一个新角色。每当我得到例外时说:
名称不能为空或空
型号:
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&#39; User_Manager_Interface.Models.ApplicationRole
的映射和元数据信息
回到上面的 1。,我之所以选择 2。,是因为 1。,应用程序不会终止,而是抛出模型状态错误(如问题标题中所述)关于Name
属性为null。
我尝试检查堆栈跟踪以获取有关异常的更多详细信息,但除了我已经知道的内容之外,我无法收集更多信息:
e.StackTrace&#34;
at System.Data.Entity.Core.Objects.ObjectContext.GetTypeUsage(Type entityCLRType)\ r \ n
at System.Data.Entity.Core.Objects.ObjectContext.ValidateEntitySet(EntitySet entitySet,Type entityType)\ r \ n
at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach,String entitySetName,IEntityWrapper wrappedEntity,EntityEntry existingEntry,EntitySet&amp; entitySet,Boolean&amp; isNoOperation)\ r \ n
at System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName,Object entity)\ 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.InternalSet
在System.Data.Entity.Internal.Linq.InternalSet1.Add(Object entity)\r\n
1.添加(TEntity实体)\ r \ n
at System.Data.Entity.DbSet
在Microsoft.AspNet.Identity.EntityFramework.RoleStore`3.d__2.MoveNext()\ r \ n 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
1.GetResult()\ r \ n在C:\ Users \ Linda \ UserManager \ FSKUserManager \ FSK_UserManager_Web \ Controllers \中的User_Manager_Interface.Controllers.RolesController.d__5.MoveNext() RolesController.cs:第104行&#34;
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
答案 0 :(得分:3)
我认为它与属性声明中的new
关键字有关。如果IdentityRole
已经拥有这些属性,请从ApplicationRole
模型中删除以下属性
public new string Id { get; set; }
[Display(Name = "Name")]
public new string Name { get; set; }
<强>更新强>:
在ApplicationRole
课程中,通过使用Id
关键字声明Name
和new
属性,您实际上隐藏了基类(IdentityRole
)属性。因此,每次尝试保存新记录时,这些属性都为空,您将收到相应的错误。
同时从
更改此行var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
要
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(db));