我有一个应用程序,我在 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; }
}
图像:
在发布此内容之前,我尝试了一些操作,并希望它们删除所有身份生成的表,但它不起作用,现在我收到上面的错误,该错误不允许我创建任何内容。
我对身份还很陌生,可能做错了什么。
我在这里缺少什么?
编辑:
在 @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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3327303f333b1e33273b333f3732703d3133" rel="noreferrer noopener nofollow">[email protected]</a>";
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/