首页 文章

(MVC,Identity)如何将ApplicationUser添加到模型中?

提问于
浏览
1

我想在MVC中做一个Blog项目 . 所以我在VS whit ASP.NET 4.5.1和个人用户帐户中创建了一个新项目 .

现在我面临与下面文章中提到的完全相同的问题:

ASP.NET MVC 5 - Identity. How to get current 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个类都是内置类,即它们是在创建项目期间构建的 . )

我该怎么办?

1 回答

  • 0
    public string AuthorId { get; set; }
    [ForeignKey("AuthorId")]
    public ApplicationUser Author { get; set; } //this line is the problem!
    

相关问题