Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解决错误“在依赖类型上找不到外键名”_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 5_Identity - Fatal编程技术网

C# 如何解决错误“在依赖类型上找不到外键名”

C# 如何解决错误“在依赖类型上找不到外键名”,c#,asp.net,asp.net-mvc,asp.net-mvc-5,identity,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Identity,我有一个应用程序,在该应用程序中,我在IDENTITY的帮助下创建了一个登录名: 在userTable中,我创建了一些自定义属性,其中有一个名为orgId的属性,可以让我知道用户属于哪个组织 我在SQLManagementStudio中创建了组织表,到目前为止一切都很好 orgId是对下面组织表映像的引用,我不知道如何将此属性设置为用户表上的forein键 每次运行应用程序时,我都会得到: 在依赖项上找不到外键名称“OrganizationId” 类型 我的代码: public class Ap

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

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

我在SQLManagementStudio中创建了组织表,到目前为止一切都很好

orgId是对下面组织表映像的引用,我不知道如何将此属性设置为用户表上的forein键

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

在依赖项上找不到外键名称“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;

    }
}
图像:

在发布这篇文章之前,我尝试了一些方法,删除了所有标识生成的表,但是没有效果,现在我得到了上面的错误,这不允许我创建任何内容

我对身份还比较陌生,可能做错了什么

我错过了什么

编辑:

在@Adil Mammadov的回答非常有用之后,我不断地发现其他错误

我当前的错误:

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

其他信息:检测到一个或多个验证错误 在模型生成过程中: ApplicationUser\u Organization\u Source::多重性在中无效 关系中的角色“ApplicationUser\u Organization\u Source” “应用程序用户组织”。因为依赖角色属性 不是键属性,而是该键的多重性的上限 从属角色必须为“*”

此外,我认为最好指出以下几点: 在我的startup.cs中,我有以下内容:

公共void配置AppBuilder应用程序 { 配置AuthApp; 创建角色控制器; }

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

        }
    }

你的型号不对。ApplicationUser模型中没有OrganizationId,但将其指定为外键。此外,还为组织添加了两个导航属性:

您的模型应如下所示:

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

也应考虑将组织名称更改为组织名称。< /P> 使现代化


这是一对多的关系。因此,在组织模型中导航权限必须是ApplicationUser的集合。

一个应用程序用户可以有多个组织吗?不,只有一个,如果他在两个组织中,这将是db中的一个新职位。编辑:到目前为止,使用将属于多个组织的场景还不存在。那么为什么要将公共虚拟ICollection组织{get;set;}添加到ApplicationUser?老实说,我不太明白身份如何使这成为一个forein键,当浏览其他问题时,我发现类似的答案包含了这一点,我正在尝试感谢你给我一个好的答案!谢谢你引导我进入链接。但是,在应用了您建议的更改后,我得到了:附加信息:无法确定类型“OverWatch.Models.Organizations”和“OverWatch.Models.ApplicationUser”之间关联的主体端。必须使用关系fluent API或数据批注显式配置此关联的主体端。@Ra3IDeN,在您的案例中,哪个是主体?哪个应用程序用户和组织是主实体?用户属于一个组织,因此我认为组织表是主实体。@Ra3IDeN,然后在ApplicationUser类中将[Required]属性添加到公共虚拟组织组织{get;set;}。如果对你有效,告诉我,我会更新我的答案。请查看。请检查我对问题的编辑。设置required属性时,我又遇到了一个错误。编辑:请现在检查
// 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; }
}