-1

Department and store model. They are a one to one relationship. when I do the (Include) only some models it brings me the complete data. The data is correctly in the database

 var orders = await _context.Order.Include(o => o.Departament)
                                  .Include(o => o.Store).ToArrayAsync();

Model Order

 public class Order
    {   [Key]
        public string Order_Id { get; set; }

        [DataType(DataType.Date)]
        public DateTime Date { get; set; }

        public string Number_Order { get; set; }  
        
        public int Qty { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime Imported { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime Modified { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime Exported { get; set; }

        public bool IsActive { get; set; }

        [ForeignKey("Departament")]
        public string DepartmentId { get; set; }
        
        [ForeignKey("Store")]
        public string StoreId { get; set; }

        public virtual ICollection<OrderDetail> OrderDetails { get; set; }
       
        public Departament Departament { get; set; }

        public Store  Store { get; set; }
    }

.

I just need to solve that problem of getting the data correctly.

  • The relationship between Order->Department and Order->Store is not One-to-One, it is Many-to-One. (Many orders could refer to the same Department) If you are hard-wiring any configuration in OnModelCreating for instance to use a HasOne/WithOne or such what you may be encountering conflicting issues where it is trying to match on the two table's PKs (which is the default behavior for One-to-One relationships) rather than the FKs. The other thing to verify is that at runtime the DbContext is actually pointing at the same database instance that your are checking against. – Steve Py Mar 31 '22 at 22:31

1 Answers1

-1
  1. what do u mean one to one!!! i think your design is one to many. as Steve Py said in his comment ## The relationship between Order->Department and Order->Store is not One-to-One, it is Many-to-One. (Many orders could refer to the same Department) ##
  2. use virtual keyword for navigation properties. accroding to https://stackoverflow.com/a/41881299/18399373 you can use virtual keyword to load related data in lazy way
  3. use explicit loading and check it. Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading
Ehsan Kenari
  • 121
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 31 '22 at 21:50
  • Thanks i think was one to one. is one to many. Thanks Thanks. – Yamgel Cruz Mar 31 '22 at 23:15