1

I want to do a Blog project in MVC. so i created a new project in VS whit ASP.NET 4.5.1 and Individual User Accounts.

Now I'm facing exactly the same problem as mentioned in article below:

ASP.NET MVC 5 - Identity. How to get current ApplicationUser

But mine is a bit deeper!

I don't know how to define a "ApplicationUser" in my Model. this is my Model:

public class Article {
    public int Id {get; set;}
    .
    .
    .
    public string AuthorId { get; set; }
    public virtual ApplicationUser Author { get; set; } //this line is the problem!
}

This is my very simple Controller:

namespace Blog.Controllers
{
    [Authorize]
    public class ArticleController : Controller
    {
        DatabaseContext db = new DatabaseContext();

        public ActionResult Index()
        {
            IEnumerable<Article> articles = db.Articles;
            return View(articles);
        }
    }
}

And i always get this set of errors:

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.

(Please note that all the 4 classes mentioned in errors above are built-in classes, i.e. they were built during creation of project.)

What should i do?

Community
  • 1
  • 1
Ali
  • 99
  • 1
  • 11

1 Answers1

0
public string AuthorId { get; set; }
[ForeignKey("AuthorId")]
public ApplicationUser Author { get; set; } //this line is the problem!
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value – Vishal Chhodwani Apr 10 '18 at 05:28