Home > Software engineering >  Referencing ApplicationUser from Model .net core
Referencing ApplicationUser from Model .net core

Time:12-21

I'm working on a project and I have two objects Group and ApplicationUser with a reference from Group to ApplicationUser. My Group model has a property CreatedBy to provide the info about the user who created the group.

Saving a group is executed correctly, CreatedBy propery storing userId in the database. The issue happens when I want to display a list of Group (objects). The CreatedBy property isn't being retrieved, instead it has null value (despite saved userId value).

Group Model:

public class Group
{
        public int Id { get; set; }
        public string Name { get; set; }
        public string Descriotion { get; set; }
        public ApplicationUser CreatedBy { get; set; }
}

Group Controller (has save and display methods):

public class GroupController : Controller
{
    private readonly ApplicationDbContext _db;
    private readonly UserManager<IdentityUser> _userManager;


    public GroupController(ApplicationDbContext db, UserManager<IdentityUser> userManager)
    {
        _db = db;
        _userManager = userManager;
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,Name,Descriotion")] Group @group)
    {
        string userId = _userManager.GetUserId(User);
        var appUser = _db.ApplicationUser.Find(userId);
        if (!String.IsNullOrEmpty(userId) && appUser != null)
        {
            group.CreatedBy = appUser;
        }


        if (ModelState.IsValid)
        {
            _db.Add(@group);
            await _db.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(@group);
    }

    public async Task<IActionResult> Groups()
    {
        return View("Groups", await _db.Groups.ToListAsync());
    }

}

ApplicationUser Model:

public class ApplicationUser : IdentityUser
{
    [NotMapped]
    public string RoleId { get; set; }
    [NotMapped]
    public string Role { get; set; }


    public ApplicationUser()
    {
    }
}

Migration for the Group (model):

public partial class GroupsModel : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Groups",
            columns: table => new
            {
                Id = table.Column<int>(type: "integer", nullable: false)
                    .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                Name = table.Column<string>(type: "text", nullable: true),
                Descriotion = table.Column<string>(type: "text", nullable: true),
                CreatedById = table.Column<string>(type: "text", nullable: false),
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Groups", x => x.Id);
                table.ForeignKey(
                    name: "FK_Groups_AspNetUsers_CreatedById",
                    column: x => x.CreatedById,
                    principalTable: "AspNetUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Groups_CreatedById",
            table: "Groups",
            column: "CreatedById");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Groups");
    }
}

My Context file

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions options) : base(options)
    {

    }


    public DbSet<ApplicationUser> ApplicationUser { get; set; }
    public DbSet<Group> Groups { get; set; }
    
}

1. Is there something wrong in my Models or any other files? How to retrieve Group object with CreatedBy correct value?

2. Is there a better way to set CreatedBy property Right now I need to create ApplicationUser object, in order to set the property. Is there a way just to provide userId (string) without creating ApplicationUser?

string userId = _userManager.GetUserId(User);
var appUser = _db.ApplicationUser.Find(userId);
if (!String.IsNullOrEmpty(userId) && appUser != null)
{
  group.CreatedBy = appUser;
}

CodePudding user response:

You have to include user if you want to see it

public async Task<IActionResult> Groups()
{
 return View("Groups", await _db.Groups.Include(i=>i.CreatedBy).ToListAsync());
}

EF Net 5 can create a foreign key column implicetly, but it is always good idea to include foreign key to the class explicetly

public class Group
{
        public int Id { get; set; }
        public string Name { get; set; }
        public string Descriotion { get; set; }

        public string CreatedById { get; set; }
        public virtual ApplicationUser CreatedBy { get; set; }
}

this way your code would be more simple

string userId = _userManager.GetUserId(User);
if ( !string.IsNullOrEmpty(userId))  group.CreatedById = userId;
  • Related