0

В таком виде, который с генерировал сам Entity Framework, всё работает нормально:

  public partial class man
    {
        public int Id_Men { get; set; }
        public string FIO { get; set; }
        public Nullable<DateTime> Birthday { get; set; }
        public Nullable<double> Height { get; set; }
        public Nullable<double> Weight { get; set; }

        public man()
        {

        }
        public man(string fIO, DateTime? birthday, double? height, double? weight)
        {

            FIO = fIO;
            Birthday = birthday;
            Height = height;
            Weight = weight;
        }
}

Но если я пытаюсь прописать свои get and set, то он выдает ошибку:

  namespace WPF_application.Models 
{
    using System;
    using System.Data.Entity.Migrations.Sql;
    using System.Collections.Generic;
    using System.ComponentModel;

    public partial class man : INotifyPropertyChanged
    {

        public int Id_Men { get; set; }
        public string FIO;
        public Nullable<DateTime> Birthday;
        public Nullable<double> Height;
        public Nullable<double> Weight;

        public man()
        {

        }
        public man(string fIO, DateTime? birthday, double? height, double? weight)
        {

            FIO = fIO;
            Birthday = birthday;
            Height = height;
            Weight = weight;
        }

        public string Fio
        {
            get { return FIO;}
            set
            {
                if (this.FIO == value) return;
                this.FIO = value;
                onPropertyChanged("FIO");
            }
        }

        public DateTime Birthday_
        {
            get { return (DateTime)Birthday; }
            set
            {
                if (this.Birthday == value) return;
                this.Birthday = value;
                onPropertyChanged("Birthday");
            }
        }

        public double Height_
        {
            get { return (double)Height; }
            set
            {
                if (this.Height == value) return;
                this.Height = value;
                onPropertyChanged("Height");
            }
        }

        public double Weight_
        {
            get { return (double)Weight; }
            set
            {
                if (this.Weight == value) return;
                this.Weight = value;
                onPropertyChanged("Weight");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void onPropertyChanged (string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

System.InvalidOperationException: "The entity type man is not part of the model for the current context."

Это исключение появляется когда через DbContext выгружаются данные:

   class ServiceMan
    {
        public List<man> getAllMan()
        {
            var dbContext = new peopleDbContext();

            IQueryable<man> query = dbContext.men;

            List<man> list_man = query.ToList();//

            return list_man;
        }       

    }

    public partial class peopleDbContext : DbContext
    {
        public peopleDbContext()
            : base("name=db_peopleEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<man> men { get; set; }
    }
}

Почему это происходит и можно ли это как-то исправить?

7
  • 1
    Сделайте поля приватными, а свойствам измените имена, чтобы они совпадали с моделью. 5 мая 2020 в 8:21
  • Пробую: public int Id_Men { get; set; } private string FIO; private DateTime Birthday; private Nullable<double> Height; private Nullable<double> Weight;
    – Vlad-i-mir
    5 мая 2020 в 8:53
  • public string Fio { get { return FIO;} set { if (this.FIO == value) return; this.FIO = value; onPropertyChanged("FIO"); } }
    – Vlad-i-mir
    5 мая 2020 в 8:55
  • public DateTime Birthday_ { get { return (DateTime)Birthday; } set { if (this.Birthday == value) return; this.Birthday = value; onPropertyChanged("Birthday"); } }
    – Vlad-i-mir
    5 мая 2020 в 8:56
  • 1
    Свойство Birthday, поле _birthday. 5 мая 2020 в 8:59

0

Ваш ответ

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

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