domikuras
domikuras

Reputation: 33

DbSet property of type class returns null

I'm creating an API for an app. The DbContext I have trouble with looks like this:

public class SchoolPlannerDbContext : DbContext
{
    public SchoolPlannerDbContext(DbContextOptions<SchoolPlannerDbContext> options) : base(options) { }
    public DbSet<Activity> Activities { get; set; }
    public DbSet<Room> Rooms { get; set; }
    public DbSet<Subject> Subjects { get; set; }
    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Group> Groups { get; set; }

}

The Activity class is as follows:

public class Activity
    {
        public int ID { get; set; }

        [Required]
        public Teacher Teacher { get; set; }

        [Required]
        public Room Room { get; set; }

        [Required]
        public Subject Subject { get; set; }

        [Required]
        public Group Group { get; set; }

        [Required]
        public int Slot { get; set; }

        [Required]
        public int Day { get; set; }
    }

All the other properties contain an int ID and a string Name.

My controller looks like this:

public class SqlPlannerData : ISchoolPlannerData
    {
        private readonly SchoolPlannerDbContext db;

        public SqlPlannerData(SchoolPlannerDbContext db)
        {
            this.db = db;
        }
        public IEnumerable<Activity> GetActivities()
        {
            return db.Activities;
        }
        public IEnumerable<Group> GetGroups()
        {
            return db.Groups;            
        }
   }

GetGroups() works as intended and returns an IEnumerable with properties set correctly.

My problem is that when I'm trying to access db.Activities, the properties of type, say, Teacher (non-basic types like int) are set to null: Debugger screenshot.

However, there is a row in the database that looks like this. I.e. the columns exist in the database.

What do I do to make GetActivities() return an IEnumerable with correctly set properties?

Upvotes: 3

Views: 657

Answers (1)

Serge
Serge

Reputation: 43860

Some properties are null because of lazy loading you need to include them

    return db.Activities
             .Include(i => i.Teacher)
              .Include(i => i.Room)
            .Include(i => i.Subject)
              .Include(i => i.Group)
                   .ToList()

Each propety Id can be configured by EF5+ as shadows. But I usually prefer to add all Ids explicitely. This way I have much less problem when I am using db context in the project. But is is optional and you can leave it as is

public class Activity
    {
        public int ID { get; set; }

        [Required]
        public int?  TeacherId { get; set; }

        [Required]
        public int? RoomId  { get; set; }

        [Required]
        public  int? SubjectId { get; set; }

        [Required]
        public int? GroupId { get; set; }


        public virtual Teacher Teacher { get; set; }

       
        public virtual Room Room { get; set; }

     
        public virtual Subject Subject { get; set; }

       
        public  virtual Group Group { get; set; }

       
        [Required]
        public int Slot { get; set; }

        [Required]
        public int Day { get; set; }
    }

and in order to get list activities you have to add ToList() or ToArray() at least

 public IEnumerable<Activity> GetActivities()
        {
            return db.Activities.ToArray();
        }

and by the way, you can' t using not nullabe Id as required becaue it is relevant

 [Required]
 public int  TeacherId { get; set; }

since int by default is 0 and it is a valid value and required will not be working

Upvotes: 0

Related Questions