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



我想用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!

相关内容

  • 没有找到相关文章

最新更新