Разработка связывания таблиц с использованием аннотаций для EF Core - PullRequest
0 голосов
/ 01 мая 2020

Я теряюсь, когда пытаюсь установить связи между моими таблицами.

Мой первый и самый простой вопрос - какова цель HashSet в основном определении?

I иметь таблицу Customer с двумя полями, связанными с таблицей Contact, MainContact и AccountsContact. Я могу связать их, как показано в коде ниже.

Я также хочу иметь возможность хранить общие контакты в таблице Contacts, которые ссылаются на Customer. Это где я заблудился. Если я добавлю поле CustomerId в таблицу Contacts, у меня возникнут проблемы, когда EF не может определить отношения или может оказаться вовлеченным в бесконечное l oop возможно.

Это исключение, которое я получаю: Unable to determine the relationship represented by navigation property 'Contact.Customers' of type 'ICollection<Customer>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

Может ли кто-нибудь помочь мне определить это отношение, пожалуйста.

    [Table("Customer", Schema = "travel")]
    public partial class Customer
    {
        public Customer()
        {
            //What is the purpose of this HashSet? 
            //MainContact = new HashSet<Contact>();
        }
        [Key]
        public int CustomerId { get; set; }
        public string CompanyName { get; set; }

        [ForeignKey("Contact")]
        public int? MainContactId { get; set; }

        [ForeignKey("Contact")]
        public int? AccountsContactId { get; set; }

        public virtual Contact MainContact { get; set; }
        public virtual Contact AccountsContact { get; set; }
    }

    [Table("Contact", Schema = "travel")]
    public partial class Contact
    {
        public Contact()
        {
        }
        [Key]
        public int ContactId { get; set; }
        public string Name { get; set; }

        [ForeignKey("Customer")]
        public string CustomerId { get; set; }

        public virtual ICollection<Customer> Customers
    }


...