0

I have added an OrganisationId field to AspNetUsers table which is a ForeignKey from Organisation Table. When I display the list of users, I need to add a column Organisation Name to the table display.

This is the IdentityModels

using C2T.Model;

namespace C2T.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public int OrganisationId { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public virtual Organisation Organisations { get; set; }
    }

    public class ApplicationRole : IdentityRole
    {
        public ApplicationRole() : base() { }
        public ApplicationRole(string name) : base(name) { }
        public string Description { get; set; }

    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public System.Data.Entity.DbSet<C2T.Models.ApplicationRole> IdentityRoles { get; set; }
        public System.Data.Entity.DbSet<C2T.Models.RoleViewModel> RoleViewModels { get; set; }
        public System.Data.Entity.DbSet<C2T.Model.Organisation> Organisations { get; set; }
    }
}

Here is the controller which I include the Organisation in the query, however, I got the error saying Invalid object name 'dbo.Organisations'

var selectedUserIds = from user in UserManager.Users
                                  select user;
            selectedUserIds.Include(x => x.Roles).Include(x=>x.Organisations).ToList();

I have Organisation table in the database. Can anyone tell me whats wrong on this one? Thank you in advance.

Jen143
  • 815
  • 4
  • 17
  • 42

1 Answers1

1

Not sure which version of Identity you are using, but I will assume 3.0 or 4.0. The link posted by Ehasanul as a comment to your question is for an older version and wouldn't help anyway.

To actually address your error, it looks like your code is not aware that the table exists in the database. Do you use Migrations? If so, need to apply that.

The other possibility is that you don't have a proper model for Organisations in your code. You didn't include it in your question, which makes it difficult to know if you do.

You have both of these lines:

public int OrganisationId { get; set; }
public virtual Organisation Organisations { get; set; }

in your ApplicationUser model, which indicates that ApplicationUser is a child to Organisation. If that's the case, your Organisation model should look something like this:

namespace C2T.Models
{
    public class Organisation
    {
        public int OrganisationId { get; set; }

        // Whatever other fields you want
        public string NameOrOtherField { get; set; }

        public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
    }
}
Malcolm T.
  • 33
  • 6