[英]How to properly add a foreign key from my own model to the EF Userprofile?
my model classes first: 首先我的模型课:
public class Person
{
[Required, Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string LearntSkillsAndLevelOfSkills { get; set; }
public string ProfileImage { get; set; }
public string City { get; set; }
public string PhoneNr { get; set; }
[Required]
public string Email { get; set; }
public string Hobbys { get; set; }
public string SkillsToLearn { get; set; }
public string Stand { get; set; }
public int YearsOfWorkExperience { get; set; }
public string HobbyProjectICTRelated { get; set; }
public string ExtraInfo { get; set; }
public string Summary { get; set; }
public int UserId { get; set; }
[ForeignKey("UserId")]
public virtual UserProfile profile { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public Nullable<int> ID { get; set; }
public virtual Person personprofile { get; set; }
}
when i run this however it gives me this exception: The principal end of this association must be explicitly configured 当我运行它时,它给了我这个例外:必须显式配置此关联的主体端
i've searched this error but it doesn't clarify it for me... so i absolutely have no clue how to fix this. 我已经搜索了此错误,但并没有为我澄清它。因此,我绝对不知道如何解决此错误。 Basically i want to link my Person class to the Userprofiles so that i can create a login mechanism that automatically lets 1 person who makes an account on the site get 1 Profile to Edit to his own information. 基本上,我想将Person类链接到Userprofiles,这样我就可以创建一种登录机制,该机制可以自动使在该网站上进行帐户的1个人获得1个人资料以对其自己的信息进行编辑。 He's however not allowed to modify other people their accounts. 但是,不允许他修改其他人的帐户。
I hope this makes my problem clear and that somebody can help me :). 我希望这可以使我的问题变得清楚,希望有人可以帮助我:)。 i'm using EF 6 btw and i get the error in the class InitializeSimpleMembershipAttribute that comes standard with the MVC example of ASP.net 我正在使用EF 6 btw,并且在ASP.net的MVC示例附带的类InitializeSimpleMembershipAttribute中收到错误
Greetings and thanks in advance, 预先致以问候和感谢,
Marijn 马林
I managed to get it working with these codes in the models: 我设法使其与模型中的以下代码一起使用:
[Table("UserProfile")]
public class UserProfile
{
[Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual Person personprofile { get; set; }
}
public class Person
{
[ForeignKey("profile"), Key]
public int UserId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string LearntSkillsAndLevelOfSkills { get; set; }
public string ProfileImage { get; set; }
public string City { get; set; }
public string PhoneNr { get; set; }
[Required]
public string Email { get; set; }
public string Hobbys { get; set; }
public string SkillsToLearn { get; set; }
public string Stand { get; set; }
public int YearsOfWorkExperience { get; set; }
public string HobbyProjectICTRelated { get; set; }
public string ExtraInfo { get; set; }
public string Summary { get; set; }
public UserProfile profile { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.