简体   繁体   中英

Avoiding duplicates in table

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:

查询结果图片

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM