0

I am issuing a very strange scenario using Code first with existing database and asp.net identity entity framework. I have a simple userprofile model

    [Table("CSUserProfile")]
    public partial class UserProfile
    {
        [Key]
        public string Id { get; set; }

        [Required]
        [Display(Name = "FirstName")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "LastName")]
        public string LastName { get; set; }

        [Required]
        [Display(Name = "Phone")]
        public string Phone { get; set; }

        [Required]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Location")]
        public string Location { get; set; }

        [Required]
        [Display(Name = "HomeTown")]
        public string Hometown { get; set; }

        public byte[] BlobData { get; set; }

        [ForeignKey("fPersonLinkGID")]
        public virtual List<ProfilePic> ProfilePic { get; set; }
    }     

and an image profile pic

 [Table("CSProfilePic")]
public partial class ProfilePic
{
    [Key]
    public Guid? GID { get; set; }
    public string fPersonLinkGID { get; set; }
    public byte[] BlobData { get; set; }
}

the foreign key is the fPersonLinkGID. everything works fine but my problem is that if i want an one-to-one relation between the userprofile and the image like this

public virtual ProfilePic ProfilePic { get; set; }

(which is the correct scenario) I am getting this strange exception :

The ForeignKeyAttribute on property 'ProfilePic' on type 'eUni.Model.Application.UserProfile' is not valid. The foreign key name 'fPersonLinkGID' was not found on the dependent type 'eUni.Model.Application.UserProfile'. The Name value should be a comma separated list of foreign key property names.

I can not understand why I am getting that exception

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Antho
  • 181
  • 11
  • in EF for a on to one relation the keys have to be of the same type. That is your ProfilePic must have an Int as key. For a userId = x, you will then have a profilPicId = x. – tschmit007 Oct 20 '14 at 08:58
  • The keyes are of the same type (string,string) there is no problem using one to many relationship. my problem occurs in one to one – Antho Oct 20 '14 at 09:02
  • the PK must be of the same type: GUID is not the same as String. You may be need the same name. – tschmit007 Oct 20 '14 at 09:08
  • Primary key in UserProfile is id (string) the foreign key in ProfilePic is fPersonLinkGID which is also string, as you can see i declare in dataannotation atribute [ForeignKey("fPersonLinkGID")] public virtual List ProfilePic { get; set; } so the keys are the same type – Antho Oct 20 '14 at 09:30
  • 2
    in a one to one relation (with ef), the FK must be the PK – tschmit007 Oct 20 '14 at 09:32

1 Answers1

1

You could read this answer. It introduces how to configure one to one relationship by HasRequired and WithOptional.

As for me, I will create one to one relationship by following way.

public class Store {
    [Key]
    public long Id { get; set; }

    public virtual Item TheItem { get; set; }

    // .... 
}

public class Item {
    // It is FK, and also PK.
    [Key, ForeignKey("TheStore")]
    public long Id { get; set; }

    // The same string in the ForeignKey attribute. Ex: ForeignKey("TheStore")
    public virtual Store TheStore { get; set; }

    // ....
}
Community
  • 1
  • 1
AechoLiu
  • 17,522
  • 9
  • 100
  • 118