【问题标题】:How to solve error " The foreign key name was not found on the dependent type "如何解决错误“在依赖类型上找不到外键名称”
【发布时间】:2017-01-06 09:49:29
【问题描述】:

我有一个应用程序,我在 IDENTITY 的帮助下创建了一个登录:

在 userTable 中,我创建了一些自定义属性,其中我有一个名为“orgId”的属性,它可以让我知道用户属于哪个组织。

我在 sql-managment studio 中创建了组织表,到目前为止一切都很好。

orgId 是对组织表的引用(下图),我不知道如何使该属性成为用户表上的外键。

每次我运行应用程序我都会得到:

在依赖项上找不到外键名称“OrganizationId” 输入

我的代码:

public class ApplicationUser : IdentityUser
{
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    [ForeignKey("OrganizationId")]
    public Organizations OrgId { get; set; }

    public virtual ICollection<Organizations> Organizations { 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

        userIdentity.AddClaim(new Claim("FirstName", this.FirstName));

        return userIdentity;

    }
}

组织类:

public class Organizations
{
    public long OrganizationId { get; set; }
    public string OrganizationName { get; set; }

    public virtual ApplicationUser User { get; set; }
}

图片:

在发布此之前,我尝试了一些事情,他们正在删除所有身份生成的表,但它没有工作,现在我得到了上面的错误,它不允许我创建任何东西。

我对身份还很陌生,可能做错了什么。

我在这里错过了什么?

编辑:

在@Adil Mammadov 回答非常有帮助之后,我不断收到其他错误。

我当前的错误:

类型异常 'System.Data.Entity.ModelConfiguration.ModelValidationException' 发生在 mscorlib.dll 中,但未在用户代码中处理

附加信息:检测到一个或多个验证错误 在模型生成期间: ApplicationUser_Organization_Source: : 多重性在 关系中的角色“ApplicationUser_Organization_Source” 'ApplicationUser_Organization'。因为依赖角色属性 不是关键性质,多重性的上限 从属角色必须是“*”。

我认为最好指出以下几点: 在我的 startup.cs 我有这个:

public void 配置(IAppBuilder 应用程序) { 配置身份验证(应用程序); createRolesandUsers(); }

    private void createRolesandUsers()
    {
        ApplicationDbContext context = new ApplicationDbContext();

        var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new ApplicationDbContext()));
        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

        // In Startup iam creating first Admin Role and creating a default Admin User    
        if (!roleManager.RoleExists(OverWatchRoles.SuperDeveloper.ToString()))
        {

            // first we create Admin rool   
            var role = new ApplicationRole();
            role.Name = "SuperDeveloper";

            roleManager.Create(role);

            //Here we create a Admin super user who will maintain the website                  

            var user = new ApplicationUser();
            user.UserName = "UserName";
            user.Email = "myname@myemail.com";
            user.FirstName = "Me";
            user.LastName = "Info";
            user.OrgId = 0;

            string userPWD = "123MyPassWord!'#";

            var chkUser = UserManager.Create(user, userPWD);

            //Add default User to Role Admin   
            if (chkUser.Succeeded)
            {
                var result1 = UserManager.AddToRole(user.Id, OverWatchRoles.SuperDeveloper.ToString());
            }
        }

        if (!roleManager.RoleExists("Developer"))
        {
            var role = new ApplicationRole();
            role.Name = "Developer";
            roleManager.Create(role);

        }

        if (!roleManager.RoleExists("SuperAdministrator"))
        {
            var role = new ApplicationRole();
            role.Name = "SuperAdministrator";
            roleManager.Create(role);

        }

        if (!roleManager.RoleExists("Administrator"))
        {
            var role = new ApplicationRole();
            role.Name = "Administrator";
            roleManager.Create(role);

        }

        // creating Creating Employee role    
        if (!roleManager.RoleExists("Employee"))
        {
            var role = new ApplicationRole();
            role.Name = "Employee";
            roleManager.Create(role);

        }
    }

【问题讨论】:

  • 一个应用程序用户可以拥有多个组织吗?
  • 不,只有一个,如果他在两个组织中,这将是数据库中的一个新帖子。编辑:到目前为止,不存在使用属于多个组织的场景。
  • 那你为什么把public virtual ICollection&lt;Organizations&gt; Organizations { get; set; }加到ApplicationUser呢?
  • 老实说,我不太明白身份如何使它成为外键,在浏览其他问题时,我发现包含该问题的类似答案,我正在试验

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


【解决方案1】:

您的模型不正确。您的ApplicationUser 模型中没有OrganizationId,但您将其指定为外键。此外,您还为Organization 添加了两个导航属性:

// Yes, this is a navigation property
public Organizations OrgId { get; set; } 

// This is also navigation property
public virtual ICollection<Organizations> Organizations { get; set; }

您的模型应该如下所示:

public class ApplicationUser : IdentityUser
{
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]    
    public long OrgId { get; set; }

    // Indicates that OrgId is foreign key for Organization navigation property
    [ForeignKey("OrgId")] 
    public virtual Organizations Organization { get; set; }

    ....
}   

public class Organizations
{
    [Key]
    public long OrganizationId { get; set; }
    public string OrganizationName { get; set; }

    public virtual ICollection<ApplicationUser> Users { get; set; }
}

还可以考虑将Organizations 类的名称更改为Organization

更新。

这是一对多的关系。所以,Organization 模型导航属性必须是ApplicationUser 的集合。

【讨论】:

  • 感谢您的精彩回答!并感谢您引导我访问链接。但是,在应用您建议的更改后,我得到:“其他信息:无法确定类型 'OverWatch.Models.Organizations' 和 'OverWatch.Models.ApplicationUser' 之间关联的主体端。此关联的主体端必须使用关系流式 API 或数据注释显式配置。"
  • @Ra3IDeN,在你的情况下哪个是主要的? ApplicaitonUserOrganization 哪个是主要实体?
  • 用户属于一个组织,所以我会说组织表是主要的。
  • @Ra3IDeN,然后在ApplicationUser 类中将[Required] 属性添加到public virtual Organizations Organization { get; set; }。如果它对你有用,请告诉我,所以我会更新我的答案。看看this answer
  • 请检查我对问题的编辑。设置所需属性时出现另一个错误。编辑:请立即检查
猜你喜欢
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2013-04-29
  • 2010-10-07
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多