0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • 1
    Possible duplicate of [Implementing Zero Or One to Zero Or One relationship in EF Code first by Fluent API](http://stackoverflow.com/questions/14701378/implementing-zero-or-one-to-zero-or-one-relationship-in-ef-code-first-by-fluent) – James P May 10 '16 at 10:05
  • Other duplicate: http://stackoverflow.com/q/26389707/861716, but using `HasOptional` instead of `HasRequired`. – Gert Arnold May 10 '16 at 20:57

1 Answers1

0

This could be a cyclical reference as you have properties in each model pointing back to each other. Item <=> ItemContactInfo. Get rid of the Contact property in the Item model.

ItemId in the ItemContactInfo table would need to be the key as well as a foreign key to enforce the 0 or 1 relationship.