Sedat Sayer
Sedat Sayer

Reputation: 23

How do I save data in related tables?

I have a question. I created a table with many to many relationships as below. What code should I write so that I can enter multiple categories when adding a product to the database? I would be glad if you explain with an example. For example, I can enter product.name information with the name information I received from the user, but I do not know how to save data in the relevant tables.

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public List<ProductCategory> ProductCategories { get; set; }
        
}


public class Product
{
    public int ProductId { get; set; }  
    public string Name { get; set; }       
    public string Url { get; set; }       
    public double? Price { get; set; } 
    public string Description { get; set; }         
    public string ImageUrl { get; set; }
    public bool IsApproved { get; set; }
    public bool IsHome { get; set; }

    public List<ProductCategory> ProductCategories { get; set; }
}
   

public class ProductCategory
{
    public int CategoryId { get; set; }
    public Category Category { get; set; }
    public int ProductId { get; set; }
    public Product Product { get; set; }
}

Upvotes: 1

Views: 1852

Answers (2)

mj1313
mj1313

Reputation: 8459

You can insert them by the join table like this:

var product = new Product { Name = "AA" };
var categories = new List<Category>
{
    new Category{ Name = "a"},
    new Category{ Name = "b"},
    new Category{ Name = "c"},
};

foreach (var category in categories)
{
    _context.ProductCategory.Add(
    new ProductCategory
    {
        Product = product,
        Category = category
    });

}
_context.SaveChanges();

Upvotes: 1

Prasad Ramireddy
Prasad Ramireddy

Reputation: 297

I am not sure which version you are using. with EF Core 5.o find an example at https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew

Models:

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }

    public ICollection<Product> Products { get; set; }
}


public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public double? Price { get; set; }
    public string Description { get; set; }
    public string ImageUrl { get; set; }
    public bool IsApproved { get; set; }
    public bool IsHome { get; set; }
    public ICollection<Category> Categories { get; set; }

}

Or If you are using EE 6 or EF Core 3.1 then your models should be like:

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }

    public ICollection<Product> Products { get; set; }
    public ProductCategory ProductCategorie { get; set; }

}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public double? Price { get; set; }
    public string Description { get; set; }
    public string ImageUrl { get; set; }
    public bool IsApproved { get; set; }
    public bool IsHome { get; set; }
    public ICollection<Category> Categories { get; set; }
    public ProductCategory ProductCategorie { get; set; }

}

public class ProductCategory
{
    public int CategoryId { get; set; }
    public Category Category { get; set; }
    public int ProductId { get; set; }
    public Product Product { get; set; }
}

For inserting data to Many-Many tables: https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/complex-data-model?view=aspnetcore-5.0
then please look at the section:Seed database with test data

Upvotes: 0

Related Questions