Home > Software engineering >  Retrieve Entity Framework Foreign Key Relationship Models automatically
Retrieve Entity Framework Foreign Key Relationship Models automatically

Time:11-07

I am working on a Restaurant Application. I have a restaurant model and a table model.

namespace Restaurant.Models
{
    [Table("Restaurant")]
    public class RestaurantModel
    {
        [Key]
        [Column("id")]
        public int Id { get; set; }
        
        [Column("name")]
        public string Name { get; set; }
        
        [Column("telephone_number")]
        public int TelephoneNumber { get; set; }
        
        [NotMapped]
        public List<TableModel> Tables;
        
        public RestaurantModel()
        {
            Tables = new List<TableModel>();
        }
    }
}
namespace Restaurant.Models
{
    [Table("Table")]
    public class TableModel
    {
        [Key]
        [Column("id")]
        public int Id { get; set; }

        [ForeignKey("restaurant_id")]
        [Required] [NotNull]
        public int RestaurantId { get; set; }
        
        [Column("available_seats")]
        public int AvailableSeats { get; set; }
        
        [Column("is_indoors")]
        public bool IsIndoors { get; set; }
    }
}

I have a dependency between Restaurant and Table: enter image description here

Here are the columns and keys that Entity Framework has created for me via my context: enter image description here

Lastly, here's my Context class:

namespace Restaurant.Data
{
    public class RestaurantContext : DbContext
    {
        public RestaurantContext(DbContextOptions<RestaurantContext> options) : base(options)
        {
        }
        
        public DbSet<RestaurantModel> Restaurants { get; set; }
        public DbSet<TableModel> Tables { get; set; }
        public DbSet<GuestModel> Guests { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<RestaurantModel>().ToTable("Restaurant");
            modelBuilder.Entity<TableModel>().ToTable("Table");
            modelBuilder.Entity<GuestModel>().ToTable("Guest");

            modelBuilder.Entity<TableModel>()
                .HasOne<RestaurantModel>();
        }
    }
}

When I retrieve a restaurant, I want the corresponding tables to be retrieved inside of the TableModel List. Currently, when I retrieve a Restaurant, it will not retrieve any corresponding Tables. This makes sense to me, as I have not properly connected the relationship for EntityFramework to recognize it. I have tried to look online how to do it, consulting guides on setting up Foreign Key relationships and such. I cannot find the information I am looking for, due to a lack of basic knowledge. The answers I can find do not make sense to me because I do not understand what they are doing or how they are doing it.

Could anyone point me in the right direction or tell me what I am doing wrong?

CodePudding user response:

add relations to your classes

    [Table("Restaurant")]
    public class Restaurant
    {
        [Key]
        [Column("id")]
        public int Id { get; set; }
        
        [Column("name")]
        public string Name { get; set; }
        
        [Column("telephone_number")]
        public int TelephoneNumber { get; set; }
      
        public virtual ICollection<Table> Tables { get; set; }
        
    }
}

    [Table("Table")]
    public class Table
    {
        [Key]
        [Column("id")]
        public int Id { get; set; }

         
        public int? RestaurantId { get; set; }
         public virtual Restourant Restaurant { get; set; }

        [Column("available_seats")]
        public int AvailableSeats { get; set; }
        
        [Column("is_indoors")]
        public bool IsIndoors { get; set; }
    }
}

and since you are using Net core 5 I don' t think that you any navigation attributes or fluent APIs

Delete old migration folde and make a clean migration to db

after this you can try this code for test

var restourant= context.Restourants.Include(r=> r.Tables).FirstOrDefault(r=>r.Id==id);

it should return a restourant with a list of tables

  • Related