【发布时间】:2015-05-23 06:25:16
【问题描述】:
在我的模型中,我有两个继承自 ApplicationUser 的类
public class ApplicationUser : IdentityUser
{
[DataType(DataType.MultilineText)]
public string SelfIntroduction { get; set; }
}
public class Author : ApplicationUser
{
public ICollection<Paper> UploadedPapers { get; set; }
}
public class PCMember : ApplicationUser
{
public int? Quota { get; set; }
public ICollection<Topic> PreferredTopics { get; set; }
}
但是,我不能在 Controller 中使用这两个类。
[HttpPost]
[Authorize(Roles = "PCMember")]
public ActionResult FinishRegister(string userName, int[] selectedTopics)
{
var user = UserManager.FindByName(userName);
string x = ((PCMember)user).UserName;
// create topics
for (int i = 0; i < selectedTopics.Count(); i++)
{
Topic t = context.Topics.Find(selectedTopics[i]);
System.Diagnostics.Debug.Write(t.TopicName);
}
//System.Diagnostics.Debug.Write(selectedTopics);
return View(context.Topics.ToList());
}
当我将用户转换为 PCMember(实际上是)时,系统会告诉我
"Additional information: Unable to cast object of type
'System.Data.Entity.DynamicProxies.ApplicationUser_XXX' to type
'PaperReviewSystem.Models.PCMember'."
这意味着我使用了错误的 UserManager 或其他东西?
【问题讨论】:
-
@barry,问题是我无法生成 ApplicationUser 子类的实例,而不是如何存储它们。我将 TPH 用于我的用户模型。如果我将用户名传递给参数,我可以得到 ApplicationUser 而不是 PCMember,这正是我所需要的。
标签: c# asp.net asp.net-mvc