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

时间:2014-10-29 03:57:13

标签: c# asp.net asp.net-mvc asp.net-identity

我想在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 :(得分:0)

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