![](/img/trans.png)
[英]The ForeignKey Attr on property SongLyrics on type SongModel is not valid. The foreign key name LyricsId was not found on the dependent type SongModel
[英]How to solve error “ The foreign key name was not found on the dependent type ”
我有一個在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;
}
}
organizationClass:
public class Organizations
{
public long OrganizationId { get; set; }
public string OrganizationName { get; set; }
public virtual ApplicationUser User { get; set; }
}
圖片:
在發布此內容之前,我嘗試了一些操作,並刪除了所有由身份生成的表,但仍無法正常工作,現在出現了上面的錯誤,該錯誤使我無法創建任何內容。
我對身份不熟悉,可能做錯了什么。
我在這里想念什么?
編輯:
@Adil Mammadov回答非常有用之后,我不斷收到其他錯誤。
我目前的錯誤:
mscorlib.dll中發生類型'System.Data.Entity.ModelConfiguration.ModelValidationException'的異常,但未在用戶代碼中處理
附加信息:在模型生成期間檢測到一個或多個驗證錯誤:ApplicationUser_Organization_Source::多重性在關系“ ApplicationUser_Organization”中的角色“ ApplicationUser_Organization_Source”中無效。 因為從屬角色屬性不是關鍵屬性,所以從屬角色多重性的上限必須為'*'。
我還想指出以下幾點:在我的startup.cs中,我有以下內容:
公共無效配置(IAppBuilder應用){ConfigureAuth(app); 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);
}
}
您的模型不正確。 您在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
集合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.