I am developing an ASP.NET MVC project. I am using Entity Framework Code First to interact with data. But I am having a problem with building one to zero-or-one relationship in Code First Approach.
This is my Item class
public class Item
{
public int Id { get; set; }
[MaxLength(90)]
public String ImagePath { get; set; }
[MaxLength(50)]
public String Name { get; set; }
[MaxLength(70)]
public String MmName { get; set; }
[MaxLength(250)]
public String Description { get; set; }
[MaxLength(300)]
public String MmDescription { get; set; }
public int TotalReviewStars { get; set; }
public int TotalReviewCount { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Gallery> Galleries { get; set; }
public virtual ItemContactInfo Contact { get; set; }
}
I want Item class to have optional Contact class. So I added "public virtual ItemContactInfo Contact" to Item class. So I can retrieve contact easily like "item.Contact" in my code.
This is my ItemContactInfo class for Contact
public class ItemContactInfo
{
public int Id { get; set; }
[MaxLength(50)]
public String GeoLocation { get; set; }
[MaxLength(200)]
public String Address { get; set; }
[MaxLength(250)]
public String MmAddress { get; set; }
[MaxLength(70)]
public String Phones { get; set; }
[MaxLength(70)]
public String MmPhones { get; set; }
[MaxLength(200)]
public String Emails { get; set; }
public int? AreaId { get; set; }
public int ItemId { get; set; }
[ForeignKey("AreaId")]
public virtual Area Area { get; set; }
[ForeignKey("ItemId")]
public virtual Item Item { get; set; }
}
Contact class will have the Item required.
So when I run the migration and update database, it is giving me this error in console.
Unable to determine the principal end of an association between the types 'AyarDirectory.Domain.Entities.Item' and 'AyarDirectory.Domain.Entities.ItemContactInfo'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
How can I fix it?