简体   繁体   English

如何将我自己的模型中的外键正确添加到EF用户简要表?

[英]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.

相关问题 如何防止EF创建外键? - How to prevent EF from creating a foreign key? 如何在没有包含在EF模型中的外键的情况下从一个表到多个表列中获取值 - How to get values from a one to many table column without foreign key included in EF model 完全不知道如何在EF框架中使用外键更新模型(迁移) - Complete clueless how update model with foreign key in EF framework (migration) 显示外键引用到用户配置文件表的表中的项目列表 - show list of items from a table that is referenced by foreign key to a userprofile table 如何使用外键将SimpleMembership UserProfile链接到扩展配置文件? - how to link SimpleMembership UserProfile to an extended profile with foreign key? 如何向 EF 核心中的现有数据添加新的外键属性? - How to add a new foreign key property to existing data in EF core? 如何在 EF Core 中的不同项目之间向 ApplicationUser 添加外键? - How to add a foreign key to ApplicationUser between separate project in EF Core? 使用EF生成的模型和我自己的模型 - Working with EF Generated Model and My Own Model EF6外键约束简单模型 - EF6 foreign key constraint in simple model 如何将第二个外键添加到EF属性中,该属性已经是复合外键的一部分 - How to add a second foreign key to an EF property which is part of a composite foreign key already
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM