Błędne relacje pomiedzy modelami

0

Cześć.

Zabrałem się w swoim projekcie za metode Seed() w której chce utworzyc przykładowe obiekty, które powinny wygenerować się w mojej bazie.

Stosuje migracje, ale otrzymuje taki błąd:

 Entities in 'ApplicationDbContext.Addresseses' participate in the 'Addresses_Customer' relationship. 0 related 'Addresses_Customer_Target' were found. 1 'Addresses_Customer_Target' is expected.

Po wpisaniu komendy: update-database lub update-database -verbose.

Model adres wygląda tak:

public class Addresses
    {
        public int Id { get; set; }
        public string Country { get; set; } // panstwo
        public string PostalCode { get; set; } // kod pocztowy
        public string City { get; set; } // miasto
        public string StreetName { get; set; } // pelna nazwa ulicy
        public string FlatNumber { get; set; } // numer mieszkania
        public virtual Customer Customer { get; set; }
        public virtual ICollection<Suplier> Suplier { get; set; }

    }

 

Model Customer

 
 public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; } // imie
        public string LastName { get; set; } // nazwisko
        public virtual ICollection<Addresses> Addresses { get; set; } // adresy uzytkownika
        public uint Age { get; set; } // wiek uzytkownika
        public string Gender { get; set; } // plec uzytkownika
        public string PhoneNumber { get; set; }
        public int TotalPremiumPoints { get; set; } // liczba punktow znizki
        public virtual ICollection<Order> Orders { get; set; }
    }

Chce żeby jeden klient miał wiele adresów. Co robię źle?

1

Ja bym to zrobił tak:

public class Addres
    {
        public int Id { get; set; }
        public string Country { get; set; } // panstwo
        public string PostalCode { get; set; } // kod pocztowy
        public string City { get; set; } // miasto
        public string StreetName { get; set; } // pelna nazwa ulicy
        public string FlatNumber { get; set; } // numer mieszkania
        public virtual ICollection<Suplier> Suplier { get; set; }
 
    }
 public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; } // imie
        public string LastName { get; set; } // nazwisko
        public virtual ICollection<Addres> Addresses { get; set; } // adresy uzytkownika
        public uint Age { get; set; } // wiek uzytkownika
        public string Gender { get; set; } // plec uzytkownika
        public string PhoneNumber { get; set; }
        public int TotalPremiumPoints { get; set; } // liczba punktow znizki
        public virtual ICollection<Order> Orders { get; set; }
    }
0

Dzięki :) Faktycznie działa.

Jeszcze we fluentMappingu musiałem usunąć
//modelBuilder.Entity<Addresses>().HasRequired(a => a.Customer);

0

Tak dodam od siebie że jeszcze możesz wstawić konstruktor do Customer:

 public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; } // imie
        public string LastName { get; set; } // nazwisko
        public virtual ICollection<Addres> Addresses { get; set; } // adresy uzytkownika
        public uint Age { get; set; } // wiek uzytkownika
        public string Gender { get; set; } // plec uzytkownika
        public string PhoneNumber { get; set; }
        public int TotalPremiumPoints { get; set; } // liczba punktow znizki
        public virtual ICollection<Order> Orders { get; set; }
   
        public Customer() 
       {
              this.Orders = new List<Order>();
              this.Addres = new List<Adres>();
       }
 }

Jeżeli używasz angielskiego to chociaż nie rób błędów :P adres po angielsku to "Address" a nie "Addres" :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1