【发布时间】: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<Organizations> Organizations { get; set; }加到ApplicationUser呢? -
老实说,我不太明白身份如何使它成为外键,在浏览其他问题时,我发现包含该问题的类似答案,我正在试验
标签: c# asp.net asp.net-mvc asp.net-mvc-5 identity