gpt4 book ai didi

c# - 如何解决错误 "The foreign key name was not found on the dependent type "

转载 作者:行者123 更新时间:2023-11-30 20:34:26 28 4
gpt4 key购买 nike

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

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

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

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

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

The foreign key name 'OrganizationId' was not found on the dependent type

我的代码:

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

图片:

enter image description here

enter image description here

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

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

我在这里错过了什么?

编辑:

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

我当前的错误:

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in mscorlib.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation: ApplicationUser_Organization_Source: : Multiplicity is not valid in Role 'ApplicationUser_Organization_Source' in relationship 'ApplicationUser_Organization'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

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

公共(public)无效配置(IAppBuilder 应用程序) { 配置授权(应用程序); 创建角色和用户();

    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,但您将其指定为外键。此外,您还为 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的集合。

关于c# - 如何解决错误 "The foreign key name was not found on the dependent type ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39221914/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com