2

Есть 2 модели. не могу понять почему это поле содержится в Destination, а не в Lodging

public List<Lodging> Lodgings { get; set;}

Я так понял это обозначения связей

Объясните, пожалуйста!

using System.Collections.Generic;

 -  namespace Model {
        public class Destination
        {
            public int DestinationId { get; set; }
            public string Name { get; set; }
            public string Country { get; set; }
            public string Description { get; set; }
            public byte[] Photo { get; set; }
           public List<Lodging> Lodgings { get; set;
       } }

namespace Model
{
    public class Lodging
    {
        public int LodgingId { get; set; }
        public string Name { get; set; }
        public string Owner { get; set; }
        public bool IsResort { get; set; }
        public Destination Destination { get; set; }
    }
}

1 ответ 1

2

Логично предположить, что это поле содержит список Lodging, которые ссылаются на данный Destination. Т.е. список всего арендуемого жилья, имеющегося в наличии по данному направлению.

Если уж используется Code First, мне кажется, правильно было бы сделать навигационные свойства так:

using System.Collections.Generic;

namespace Model 
{
    public class Destination
    {
        public int DestinationId { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
        public string Description { get; set; }
        public byte[] Photo { get; set; }

        //навигационное свойство-список
        public virtual ICollection<Lodging> Lodgings { get; set;}
    }

    public class Lodging
    {
        public int LodgingId { get; set; }
        public string Name { get; set; }
        public string Owner { get; set; }
        public bool IsResort { get; set; }
        public int DestinationId { get; set; }

        //навигационное свойство
        public virtual Destination Destination { get; set; }
    }
}

Вот здесь есть цикл статей по ASP.NET MVC, обратите внимание на главу "Code First и соглашения".

1
  • более того, без ключевого слова virtual (как в вопросе) - Lodgings не будут навигационным свойством
    – ale
    2 апр 2016 в 14:10

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.