0

I am trying to make a table that has 2 foreign keys referencing two other tables, what I have is an ItemTable and an OrderTable , I want to take the primary key of both tables and put them in OrderDetials table using asp.net EF core.

here is my itemTable.cs Model :

   public class ItemTables
    {
        [Key]
        public int Id { get; set; }

        public string company { get; set; }

        public string availability { get; set; }

        public decimal price { get; set; }

        public decimal discount { get; set; }

        public decimal tax { get; set; }

        public string description { get; set; }

        public int categoryid { get; set; }

        public categories categories { get; set; }

        //public ICollection<OrderDetials> ItemDetials { get; set; }

    }

    public class categories
    {
        [Key]
        public int categoryID { get; set; }

        public string categoryName { get; set; }

       public ICollection<ItemTables> items { get; set; }


    }

and here is my Dbcontext for itemtables :

 public class itemTableDbContext : DbContext
    {
        public itemTableDbContext(DbContextOptions<itemTableDbContext> options) : base(options)
        {
        }


        public DbSet<ItemTables>? ItemTables { get; set; }

        public DbSet<categories>? categories { get; set; }


       

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            modelBuilder.Entity<ItemTables>()
                .HasOne<categories>(c => c.categories)
                .WithMany(i => i.items)
                .HasForeignKey(f => f.categoryid);
        }
    }

OrderTable.cs model :

 public class OrderTable
    {
        [Key]
        public int Id { get; set; }

        public string OperationType { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime Date { get; set; }

        public string Address { get; set; }

        public string Custname { get; set; }

        public int NetTotal { get; set; }

        public int GrossTotal { get; set; }

        public int DiscountTotal { get; set; }

        public int TaxTotal { get; set; }

        public int QuantityTotal { get; set; }

        public int UserID { get; set; }

        public Users User { get; set; }

        //public ICollection<OrderDetials> orderDetials { get; set; }
    }

and its Dbcontext for the ordertable :

public class usersANDordersDbContext : DbContext
    {
        public usersANDordersDbContext(DbContextOptions options) : base(options)
        {
        }

        public DbSet<Users> Users { get; set; }

        public DbSet<OrderTable> orders { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<OrderTable>()
               .HasOne<Users>(u => u.User)
               .WithMany(o => o.orders)
               .HasForeignKey(f => f.UserID);

            modelBuilder.Entity<OrderTable>().ToTable("OrderTable");

            modelBuilder.Entity<Users>().ToTable("Users");
        }
    }

and here is my attempt at the OrderDetials table Model :

 [Keyless]
    public class OrderDetials
    {
        public int OrderID { get; set; }

        public OrderTable OrderTable { get; set; }

        public int ItemID { get; set; }

        public ItemTables itemTables { get; set; }

        public int Quantity { get; set; }

        public int GrossTotal { get; set; }
        
        public int DiscountAmount { get; set; }

        public int TaxAmount { get; set; }

        public int Total { get; set; }

       
    }

and its DbContext for the orderDetials :

 public class OrderDetialsDbContext : DbContext
    {
        //public OrderDetialsDbContext(DbContextOptions options) : base(options)
        //{
        //}

        //public Microsoft.EntityFrameworkCore.DbSet<OrderDetials> OrderDetials { get; set; }

        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    modelBuilder.Entity<OrderDetials>()
        //        .HasOne<OrderTable>(o => o.OrderTable)
        //        .WithMany(d => d.orderDetials).HasForeignKey(f => f.OrderID);

        //    modelBuilder.Entity<OrderDetials>()
        //        .HasOne<ItemTables>(o => o.itemTables)
        //        .WithMany(d => d.ItemDetials).HasForeignKey(f => f.ItemID);

        //    modelBuilder.Entity<OrderDetials>().ToTable("OrderDetials");

        //}

    }

I tried implementing it, but the migration gave me an error, I assume it has to do with how I am defining my model and DbContext, I am new to asp.net and EF core, if any information needed just let me know , and thanks in advance . the migration error :

Add-Migration : A parameter cannot be found that matches parameter name 'Context'.
At line:1 char:23
+ Add-Migration detials -Context OrderDetialsDbContext
+                       ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Migration], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Add-Migration
  • "(...) the migration gave me an error,(...)" Would you please share the error – TheTanic Jan 19 '23 at 14:18
  • absolutely, I just added it. – Firefly0027 Jan 19 '23 at 14:25
  • Does this answer your question? [Add-Migration : A parameter cannot be found that matches parameter name 'Context'](https://stackoverflow.com/questions/70877255/add-migration-a-parameter-cannot-be-found-that-matches-parameter-name-context) – TheTanic Jan 19 '23 at 14:28
  • I checked it already, unfortunately not , but I don't know the problem from the error, because if I do another table without foreign keys in it, the migration worked fine, that's why I am assuming it has to do with how i am implementing my model and context – Firefly0027 Jan 19 '23 at 14:31

0 Answers0