Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am learning and I create this models, could you tell me where is my mistake?

public class Order
        {


            [Key]
            [Required]
            public int OrderID { get; set; }


            [Required]
            [Display(Name = "PO No:")]
            public int OrderNO { get; set; }


            [Required]
            [Display(Name = "Order Date")]
            public DateTime OrderDate { get; set; }

            public string Subject { get; set; }
            public string Status { get; set; }


            public decimal SubTotal { get; set; }
            public decimal Vat { get; set; }

            [DisplayName("Vat Amount")]
            public decimal Vatamount { get; set; }
            [DisplayName("Grand Total")]
            public decimal GrandTotal { get; set; }

            [DisplayName("Created By")]
            public string CreatedBy { get; set; }

            [DisplayName("Created Time")]
            public DateTime TimeStamp { get; set; }
        // ----------------- End Of Order Columns --------------
        public virtual ICollection<AddOrder> AddOrders { get; set; }



public class OrderDetails
    {
        [Key]
        public int OrderDetailsID { get; set; }

       
        [Range(-100000, 100000, ErrorMessage = "Quantity must be between 1 and 100000")]
        public int Qty { get; set; }

        [Range(0.01, 999999999, ErrorMessage = "Price must be between 0.01 and 999999999")]
        public decimal Price { get; set; }

             public decimal Total { get; set; }


        public virtual ICollection<AddOrder> AddOrders { get; set; }


public class POitem
    {

        [Key]
        [Required]
        public int POitemID { get; set; }

        [Required]
        [Display(Name = "Product/Service")]
        public string ItemName { get; set; }

        [Required]
        public string Category { get; set; }


        [DisplayName("Created By")]
        public string CreatedBy { get; set; }
        [DisplayName("Created Time")]
        public DateTime TimeStamp { get; set; }

        public virtual ICollection<AddOrder> AddOrders { get; set; }
    }
}



public class Project
{
[Key]
        [Required]
        public int ProjectID { get; set; }

        
        [Required][Display(Name = "Project Name")]
        public string ProjectName { get; set; }

        
        [Required][Display(Name = "Project Code")]
        public string ProjectCode { get; set; }

        
        [Required][Display(Name = "Location")]
        public string ProjectLocation { get; set; }

        
        [Required][Display(Name = "Owner")]
        public string ProjecOwner { get; set; }

        
        
        [Phone][Required][Display(Name = "Mobile")]
        public string ContactMobile { get; set; }

                
        [Required][EmailAddress][Display(Name = "Email")]
        public string Contactemail { get; set; }

        [DisplayName("Created By")]
        public string CreatedBy { get; set; }

        [DisplayName("Created Time")]
        public DateTime TimeStamp { get; set; }

        public virtual ICollection<AddOrder> AddOrders { get; set; }

    }
}



public class Supplier
    {


        
        [Key][Required]
        public int SupplierID { get; set; }

        
        [Required][Display(Name = "Supplier Name")]
        public string SupplierName { get; set; }

        [Required]
        public string Category { get; set; }

      
        [Required][Display(Name = "Adress")]
        public string Adress { get; set; }

        
        [Required][Display(Name = "Contact Person")]
        public string Contactperson { get; set; }

        
        
        [Phone][Required][Display(Name = "Mobile")]
        public string ContactMobile { get; set; }

        
        
        [Required][EmailAddress][Display(Name = "Email")]
        public string Contactemail { get; set; }

        [DisplayName("Created By")]
        public string CreatedBy { get; set; }

        [DisplayName("Created Time")]
        public DateTime TimeStamp { get; set; }


        public virtual ICollection<AddOrder> AddOrders { get; set; }

    }
}



And here is the View model code..


 public class AddOrder
    {


     

        //----------------------
    
        public int OrderID { get; set; }
        public int OrderNO { get; set; }
        public DateTime OrderDate { get; set; }
        public string Subject { get; set; }
        public string Status { get; set; }
        public decimal SubTotal { get; set; }
        public decimal Vat { get; set; }
        public decimal Vatamount { get; set; }
        public decimal GrandTotal { get; set; }
        public string CreatedBy { get; set; }
        public DateTime TimeStamp { get; set; }

        //-------------------
    
        public int POitemID { get; set; }
        public string ItemName { get; set; }
        public List<POitem> Items { get; set; }

        //------------------

        public int ProjectID { get; set; }
        public string ProjectName { get; set; }
        public string ProjectCode { get; set; }
        //------------------
   
        public int SupplierID { get; set; }
        public string SupplierName { get; set; }
        public string Contactperson { get; set; }
        public string ContactMobile { get; set; }
        public string Contactemail { get; set; }
        //------------------
  
        public int Id { get; set; }
        public string UnitName { get; set; }
        //------------------

        public int Qty { get; set; }
        public decimal Price { get; set; }
        public decimal VAT { get; set; }
        public decimal Total { get; set; }


    }
}



Here the code of ModelBuilder


protected override void OnModelCreating(ModelBuilder modelBuilder)
       {
           //Project
           modelBuilder.Entity<AddOrder>()
               .HasKey(bc => new { bc.OrderID, bc.ProjectID });
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.Order)
               .WithMany(b => b.AddOrders)
               .HasForeignKey(bc => bc.OrderID);
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.Project)
               .WithMany(c => c.AddOrders)
               .HasForeignKey(bc => bc.ProjectID);

           //Supplier
           modelBuilder.Entity<AddOrder>()
              .HasKey(bc => new { bc.OrderID, bc.SupplierID });
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.Order)
               .WithMany(b => b.AddOrders)
               .HasForeignKey(bc => bc.OrderID);
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.Supplier)
               .WithMany(c => c.AddOrders)
               .HasForeignKey(bc => bc.SupplierID);

           //POitem
           modelBuilder.Entity<AddOrder>()
              .HasKey(bc => new { bc.OrderID, bc.POitemID });
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.Order)
               .WithMany(b => b.AddOrders)
               .HasForeignKey(bc => bc.OrderID);
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.POitem)
               .WithMany(c => c.AddOrders)
               .HasForeignKey(bc => bc.POitemID);

           //OrderDetails
           modelBuilder.Entity<AddOrder>()
              .HasKey(bc => new { bc.OrderID, bc.OrderDetailsID });
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.Order)
               .WithMany(b => b.AddOrders)
               .HasForeignKey(bc => bc.OrderID);
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.OrderDetails)
               .WithMany(c => c.AddOrders)
               .HasForeignKey(bc => bc.OrderDetailsID);


           //Unit
           modelBuilder.Entity<AddOrder>()
              .HasKey(bc => new { bc.OrderID, bc.UnitID });
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.Order)
               .WithMany(b => b.AddOrders)
               .HasForeignKey(bc => bc.OrderID);
           modelBuilder.Entity<AddOrder>()
               .HasOne(bc => bc.Unit)
               .WithMany(c => c.AddOrders)
               .HasForeignKey(bc => bc.UnitID);
       }




Is this correct way , please advice

What I have tried:

Read many articles and document, but I believe that I missed somthing
Posted
Comments
Rajeev Jayaram 12-Apr-22 11:34am    
What exactly is your question?
Majid Farhan 12-Apr-22 14:34pm    
Is my relationship code correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900