Я хочу сделать проект блога в MVC. поэтому я создал новый проект в VS с ASP.NET 4.5.1 и отдельными учетными записями пользователей.
Теперь я столкнулся с той же проблемой, о которой упоминалось в следующей статье:
ASP.NET MVC 5 - Идентичность. Как получить текущий ApplicationUser
Но мой немного глубже!
Я не знаю, как определить "ApplicationUser" в моей модели. это моя модель:
public class Article {
public int Id {get; set;}
.
.
.
public string AuthorId { get; set; }
public virtual ApplicationUser Author { get; set; } //this line is the problem!
}
Это мой очень простой контроллер:
namespace Blog.Controllers
{
[Authorize]
public class ArticleController : Controller
{
DatabaseContext db = new DatabaseContext();
public ActionResult Index()
{
IEnumerable<Article> articles = db.Articles;
return View(articles);
}
}
}
И я всегда получаю этот набор ошибок:
Blog.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
Blog.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
(Обратите внимание, что все 4 класса, упомянутые в приведенных выше ошибках, являются встроенными классами, то есть они были созданы во время создания проекта.)
Что мне делать?
public string AuthorId { get; set; }
[ForeignKey("AuthorId")]
public ApplicationUser Author { get; set; } //this line is the problem!