I am trying to 'get' an entity from a database using Entity Framework - that has a one to one relationship with another entity. There is a foreign key called "ImageId" on ContactModel, and yet I am getting this error:
'The ForeignKeyAttribute on property 'Image' on type 'Models.ContactModel' is not valid. The foreign key name 'ImageId' was not found on the dependent type 'Models.ContactModel'. The Name value should be a comma separated list of foreign key property names.'
I am obviously doing something wrong but can't figure out what.
These are my entity classes:
[Table("Contact")]
public class ContactModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Phone { get; set; }
public string Email { get; set; }
[ForeignKey("ImageId")]
public virtual ImageModel Image { get; set; }
}
[Table("ContactImage")]
public class ImageModel
{
public int Id { get; set; }
public byte[] Image { get; set; }
}
Any suggestions would be kindly appreciated.
Thanks!
If i remeber correctly you should have a property for the foreign key and a property for the image itself on the ContactModel class.
like so:
[Table("Contact")]
public class ContactModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Phone { get; set; }
public string Email { get; set; }
//Foreign key for Image
public int ImageId { get; set; }
[ForeignKey("ImageId")]
public virtual ImageModel Image { get; set; }
}
[Table("ContactImage")]
public class ImageModel
{
public int Id { get; set; }
public byte[] Image { get; set; }
}
With your current model you're saying that ContactModel have an Image of type ImageModel and it has a foreign key called ImageId, but the foreign key doesn't exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With