Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi guys i want to make the join for the many to many in efcore3.1 my case is have class product and class invoice and the function is making the initial of the invoice and then take list of product and join in inside.
as you can see the step of join in customer invoice, products should have list of ids how to do that ???
C#
<pre> ServicesResponse<InvoiceForGet> servicesResponse = new ServicesResponse<InvoiceForGet>();
            try
            {

                Invoice invoice = new Invoice
                {
                    InvoiceNote = addCustomerInvoice.InvoiceNote,
                    AdminId = addCustomerInvoice.AdminId,
                    CustomerId = addCustomerInvoice.CustomerId,
                    CompanyStoresId = addCustomerInvoice.CompanyStoresId,
                    InvoiceDate = addCustomerInvoice.InvoiceDate.Date
                };

                var result= await _drugDbContext.Products.Select(p => p.ProductId).ToListAsync();
                


                CustomerInvoice customerInvoice = new CustomerInvoice
                {
                    Products = result,
                    Invoice = invoice
                };
                await _drugDbContext.CustomerInvoices.AddAsync(customerInvoice);
                await _drugDbContext.SaveChangesAsync();
                servicesResponse.Data = _mapper.Map<InvoiceForGet>(invoice);
            }


What I have tried:

if i keep it like this give e error (
Cannot implicitly convert type 'System.Collections.Generic.List<int>' to 'DrugStore.Entities.Product'
)
i search but till now i didn't find the answer
Posted
Updated 27-Sep-20 11:37am
Comments
Sandeep Mewara 27-Sep-20 4:56am    
I can assume and reply but it would better if you share details of your Invoice, Products, CustomerInvoice class to understand how you have set them up. Use 'Improve Question' option to edit and update.
Anas92 27-Sep-20 11:15am    
i already post all the info about the classes and i make the addcustomerinvoice class and put inside of it the list of the product because i want to make the invoice and add list of product which already will be inside the database(get it and then join it to the customer invoice )

1. Your CustomerInvoice has:
C#
public Product Products { get; set; }
A single Product.

But, while creating CustomerInvoice, you provide with a list:
C#
var result= await _drugDbContext.Products.Select(p => p.ProductId).ToListAsync();
         
CustomerInvoice customerInvoice = new CustomerInvoice
{
    Products = result,
    Invoice = invoice
};

2. You are not selecting a single product but a list of productIds here:
C#
var result= await _drugDbContext.Products.Select(p => p.ProductId).ToListAsync();

You need to select the product which matches a particular ID and then use it as Product in 1 above.
 
Share this answer
 
Comments
Anas92 28-Sep-20 6:36am    
you mean for example use FirstOrDefaultAsync to take single product and inserted in invoice i already make this and it's work but the customerinvoice table the join between product and invoice and i want to make the invoice and inside of it many product (form i understand in many-many is initial both table and then make the join inside the joining table).
sorry about the edit of question i don't know about that
Sandeep Mewara 28-Sep-20 6:50am    
Then you need to change your class:
Your customerinvoice need to have:
public List<product> Products { get; set; }
Anas92 28-Sep-20 7:56am    
you mean if i want to make(function for insert)many invoice and many product i should make them both list inside the customer invoice?
Sandeep Mewara 28-Sep-20 8:07am    
I mean - currently you map CustomerInvoice with 1 product in the data model. BUT, you need one to many. So, you need to change from one to a list there first and then work accordingly.
Anas92 28-Sep-20 8:04am    
I already make it list of the product inside the customer invoice and still same error
<pre lang="c#"><pre> public class Invoice
    {
        public int InvoiceId { get; set; }
        public DateTimeOffset InvoiceDate { get; set; } = DateTimeOffset.UtcNow;
        public string InvoiceNote { get; set; }
        public int CustomerId { get; set; }
        public Customer Customer { get; set; }
        public int AdminId { get; set; }
        public Admin Admin { get; set; }
        public int CompanyStoresId { get; set; }
        public CompanyStore CompanyStores { get; set; }
        public ICollection<CustomerInvoice> CustomerInvoices { get; set; }
         = new List<CustomerInvoice>();
        public ICollection<ProductsReturn> ProductsReturn { get; set; }
         = new List<ProductsReturn>();
    }


<pre lang="c#"><pre> public class Product
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public string Company { get; set; }
        public float Price { get; set; }
        public int Quantity { get; set; }
        public string Description { get; set; }
        public string ProductImage { get; set; }
        public string BarCode { get; set; }
        public float BuyPrice { get; set; }
        public ICollection<ProductAndCategory> ProductCategory { get; set; }
= new List<ProductAndCategory>();
        public ICollection<TransportInvoice> transportInvoices { get; set; }
= new List<TransportInvoice>();
        public ICollection<CustomerInvoice> CustomerInvoices { get; set; }
       = new List<CustomerInvoice>();
        public ICollection<ProductsReturn> ProductsReturn { get; set; }
      = new List<ProductsReturn>();
        public int CompanyStoresId { get; set; }
        public CompanyStore CompanyStores { get; set; }
        public ICollection<OfficeInvoice> OfficeInvoices { get; set; }
   = new List<OfficeInvoice>();
        public ICollection<OfficeReturn> OfficeReturns { get; set; }
    = new List<OfficeReturn>();
    }

<pre lang="c#"><pre>public class CustomerInvoice
    {
        public int CustomerInvoiceId { get; set; }
        public int Quantity { get; set; }
        public float Discount { get; set; }
        public float TotalPrice { get; set; }
        public int InvoiceId { get; set; }
        public Invoice Invoice { get; set; }
        public int ProductId { get; set; }
        public Product Products { get; set; }
        public ICollection<ProductsReturn> ProductsReturn { get; set; }
       = new List<ProductsReturn>();
    }

<pre lang="c#"><pre>            modelBuilder.Entity<CustomerInvoice>(entity =>
            {
                modelBuilder.Entity<CustomerInvoice>().HasKey(ci => new { ci.ProductId, ci.InvoiceId });
                entity.HasKey(c => c.CustomerInvoiceId);
                entity.Property(c => c.Quantity).IsRequired();
                entity.Property(c => c.TotalPrice).IsRequired();
            });
            modelBuilder.Entity<CustomerInvoice>(entity =>
            {
                entity.HasOne(c => c.Products)
                       .WithMany(p => p.CustomerInvoices)
                       .HasForeignKey(c => c.ProductId)
                       .IsRequired(false)
                       .OnDelete(DeleteBehavior.NoAction);
            });
            modelBuilder.Entity<CustomerInvoice>(entity =>
            {
                entity.HasOne(c => c.Invoice)
                       .WithMany(i => i.CustomerInvoices)
                       .HasForeignKey(c => c.InvoiceId)
                       .IsRequired(false)
                       .OnDelete(DeleteBehavior.NoAction);
            });


<pre lang="c#"><pre>public class AddCustomerInvoice
    {
        public ICollection<Product> products { get; set; }
         = new List<Product>();
        public DateTime InvoiceDate { get; set; }
        public string InvoiceNote { get; set; }
        public int CustomerId { get; set; }
        public int AdminId { get; set; }
        public int CompanyStoresId { get; set; }
    }
 
Share this answer
 
Comments
Sandeep Mewara 27-Sep-20 17:37pm    
You should use 'Improve Question' option above to edit/update your query.

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