【问题标题】:Create Relationship to AspNetUsers to other table创建与 AspNetUsers 到其他表的关系
【发布时间】:2018-01-10 02:28:17
【问题描述】:

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

这是身份模型

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();

我在数据库中有组织表。谁能告诉我这个有什么问题?提前谢谢你。

【问题讨论】:

标签: c# asp.net-mvc asp.net-identity relationship


【解决方案1】:

不确定您使用的是哪个版本的 Identity,但我假设是 3.0 或 4.0。 Ehasanul 作为对您问题的评论发布的链接适用于旧版本,无论如何都无济于事。

要真正解决您的错误,您的代码似乎不知道该表存在于数据库中。你使用迁移吗?如果是这样,需要应用它。

另一种可能性是您的代码中没有合适的组织模型。您没有在问题中包含它,因此很难知道您是否包含。

你有这两行:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2020-09-13
    • 1970-01-01
    • 2019-09-04
    • 2016-11-17
    相关资源
    最近更新 更多