I master the new authorization system ASP.NET Identity and the following question arose. When using the code first approach, everything is perfectly formed, but I need to add further the Articles table, which should contain a foreign key to the AspNetUsers table (I need to pull out the name of the user who added the article, the table is formed using ASP.NET Identity), I try to do in the Article class that then type:

public Guid AspNetUserId { get; set; } public AspNetUsers AspNetUser { get; set; } 

But AspNetUsers is highlighted in red and does not see the table. Tell me how to get a connection like this. And also, will there be a problem for me in getting a user ID for entering it into the Articles table. Thank you in advance.

    1 answer 1

    I implemented it myself:

     //Этот класс уже должен быть в проекте public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); return userIdentity; } // Тут устанавливаем связь с нашей таблицей public virtual ICollection<Article> Articles { get; set; } public ApplicationUser() { Articles = new List<Article>(); } } //Модель данных "Статьи" public class Article { // ID статьи public int Id { get; set; } // название статьи public string Name { get; set; } // описание статьи public string Description { get; set; } //ASP.NET Identity использует тип string для ID пользователя public string UserId { get; set; } public virtual ApplicationUser User { get; set; } } //Контекст данных public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } public DbSet<Article> Articles { get; set; } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } 
    • Thank you so much :) worked. If it's not difficult for you to clarify, please, a few points: 1. I understand the GenerateUserIdentityAsync method, will I be used to get information about the user? And if so, what is needed for this? 2. for what public virtual ICollection <Article> Articles was declared, we get that the connection should be one-to-many, and the announcement of the collection seems to be typical for many to many. 3. public static ApplicationDbContext Create () and this is what for? Sorry in advance for stupid questions, just learning. damn questions so many questions ( - Alex_student