1

如何在实体框架的数据库中创建具有字段的外键字段?

DecodePersonnelID我想在表MyUserPersonnelIDPersonnel(字段键)之间创建关系。

public partial class MyUser
{
    public int UsersID { get; set; }
    public string PersonnelID { get; set; }
    [NotMapped]
    public int DecodePersonnelID
    {
        get { return DecodeMD5PersonnelID(PersonnelID); }
        set { PersonnelID = EncodeMD5(value.ToString() ); }
    }
    public string UsersName { get; set; }
}

public partial class Personnel
{
    public int PersonnelID { get; set; }
    public string FirstNameFa { get; set; }
    public string LastNameFa { get; set; }
    public bool Gender { get; set; }
}
4

2 回答 2

0

你可以public virtual Personnel Personnel { get; set; }在你的定义MyUser

public partial class MyUser
{
    public int UsersID { get; set; }

    public string PersonnelID { get; set; }
    [NotMapped]
    public int DecodePersonnelID
    {
        get { return DecodeMD5PersonnelID(PersonnelID); }
        set { PersonnelID = EncodeMD5(value.ToString() ); }
    }
    public string UsersName { get; set; }
    public virtual Personnel Personnel { get; set; }
}
public partial class Personnel
{
    public int PersonnelID { get; set; }
    public string FirstNameFa { get; set; }
    public string LastNameFa { get; set; }
    public bool Gender { get; set; }
}
于 2013-04-04T06:00:25.453 回答
0

如果DecodePersonnelID是表中的列,则MyUser必须以相反的方式进行操作,即PersonnelID用标记[NotMapped],然后标记DecodePersonnelID为外键:

public partial class MyUser
{
    public int UsersID { get; set; }

    [NotMapped]
    public string PersonnelID
    {
        get { return EncodeMD5(DecodePersonnelID.ToString()); }
        set { DecodePersonnelID = DecodeMD5PersonnelID(value); }
    }

    [ForeignKey("Personnel")]
    public int DecodePersonnelID { get; set; }
    public string UsersName { get; set; }

    public Personnel Personnel { get; set; }
}

public partial class Personnel
{
    public int PersonnelID { get; set; }
    public string FirstNameFa { get; set; }
    public string LastNameFa { get; set; }
    public bool Gender { get; set; }
}

这将在导航和MyUser外键属性之间创建一对多的关系。PersonnelMyUser.PersonnelDecodePersonnelID

于 2013-04-01T14:13:19.770 回答