Avoiding duplicates in table

c# entity-framework-6

Question

I have 2 Entity classes, Product and Category. Each product has a category, and a category contain many products:

public class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public Category(string name)
        {
            Name = name;
        }

        public Category()
        {
        }
    }
public class Product
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public string Title { get; set; }

        public virtual Category Category { get; set; }

        public string LongDescription { get; set; }

        public string ShortDescription { get; set; }

        public string ShortSummary { get; set; }

        public string LongSummary { get; set; }

        public virtual List<ProductFeature> Features { get; set; }

        public virtual List<ProductImage> Images { get; set; }

        public double Price { get; set; }

        public virtual ProductFamily ProductFamily { get; set; }

        public virtual Series Series { get; set; }

        public virtual Supplier Supplier { get; set; }
    }

So when I try to place it in the database like so:

using (var context = new ProductContext())
            {

                try
                {
                    int count = 0;
                    foreach (Product product in products)
                    {
                        //var category = context.Categories.SingleOrDefault(x => x.Name == product.Category.Name) ?? new Category("false");
                        if (context.Categories.Any(c => c.Name == product.Category.Name))
                        {
                            var cat = context.Categories.Where(c => c.Name == product.Category.Name).FirstOrDefault();
                            product.Category = cat;
                        }
                        context.Products.Add(product);
                        count++;
                        if (count == 10) break;
                    }

                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    context.SaveChanges();
                }
            }

I get duplicate categories in my database:

Image of query results

Not sure what im doing wrong as I try to fetch the category from the database if it exists, and place it on the product.

1
0
1/31/2020 12:19:28 PM

Accepted Answer

Add CategoryID property into your model:

public int? CategoryID { get; set; }

Then set it in your loop:

var category = context.Categories.AsNoTracking().FirstOrDefault(x => x.Name == product.Category.Name);
product.CategoryID = category?.ID;

Also you should not place context.SaveChanges() in finally section, because it will be called event if an exception happens so you will get one more unclear exception. And one more thing, try to find a way to have CategoryID properties already set when you call the method, now you set Category.Name somehow, set CategoryID instead of that.

0
1/31/2020 12:14:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow