繁体   English   中英

创建与AspNetUsers到其他表的关系

[英]Create Relationship to AspNetUsers to other table

我向AspNetUsers表添加了OrganisationId字段,该字段是Organization Table中的ForeignKey。 当显示用户列表时,需要在表显示中添加一列组织名称。

这是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; }
    }
}

这是我在查询中包括组织的控制器,但是,我收到错误消息,指出Invalid object name 'dbo.Organisations'

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

我的数据库中有组织表。 谁能告诉我这是怎么回事? 先感谢您。

不知道您使用的是哪个版本的Identity,但是我将假定为3.0或4.0。 Ehasanul发布的对您的问题的评论链接是针对较旧版本的,仍然无济于事。

要实际解决您的错误,您的代码似乎不知道该表存在于数据库中。 您是否使用迁移? 如果是这样,则需要套用。

另一种可能性是您的代码中没有针对组织的适当模型。 您没有在问题中包括它,因此很难知道是否这样做。

您具有以下两行:

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

在您的ApplicationUser模型中,表明ApplicationUser是组织的子级。 在这种情况下,您的组织模型应如下所示:

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; }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM