locked
Basic design - repeating products i category RRS feed

  • Question

  • User-821126599 posted

    Entity:

        public class Culture : Base
        {
            [ForeignKey("CategoryId")]
            public string Name { get; set; }
            public string Code { get; set; }
            public string CurrencySymbol { get; set; }
        }

        [Table("ShopCategory")]
        public class Category : ShopBase
        {
            public int StoreId { get; set; }
            public virtual Store Store { get; set; }
            public int CultureId { get; set; }
            public virtual Culture Culture { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
    
            public ICollection<Product> Products { get; set; } = new HashSet<Product>();
        }
        [Table("ShopProduct")]
        public class Product : ShopBase
        {
            public bool Active { get; set; } = true;
            public int CategoryId { get; set; }
            public Category Category { get; set; }
            
            public ICollection<ProductInfo> InfoItems { get; set; } = new HashSet<ProductInfo>();
        }
        [Table("ShopProductInfo")]
        public class ProductInfo : ShopBase
        {
            public int ProductId { get; set; }
            public int CultureId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public decimal Sum { get; set; }       
    
            public ICollection<GraphicItem> GraphicItems { get; set; }
        }

    This design keeps repeating itself cause of the Product.Category since I need to know the Culture object when displaying InfoProduct. How should I change the design??

    The idea was to access Culture throuh ProductInfo -> Product -> Category

    Wednesday, June 21, 2017 5:35 PM

All replies

  • User-707554951 posted

    Hi ftbadolato

    I need to know the Culture object when displaying InfoProduct

    For your problem, you need to config correct relationship between entities.

    You could try to refer to the following code:

     [Table("ShopCategory")]
        public class Category
        {
            public virtual Store Store { get; set; }
            public virtual Culture Culture { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public ICollection<Product> Products { get; set; } = new HashSet<Product>();
        }
        [Table("Stores")]
        public class Store
        {
            [ForeignKey("Category")]
            public int StoreId { get; set; }
            public string StoreName{ get; set; }
            public virtual Category Category { get; set; }
        }
        [Table("Cultures")]
        public class Culture
        {
            [ForeignKey("Category")]
            public int CultureId { get; set; }
            public string CultureName { get; set; }
            public virtual Category Category { get; set; }
        }
        [Table("ShopProduct")]
        public class Product 
        {
            public bool Active { get; set; } = true;
            public int CategoryId { get; set; }
            public virtual Category Category { get; set; }
            public ICollection<ProductInfo> InfoItems { get; set; } = new HashSet<ProductInfo>();
        }
        [Table("ShopProductInfo")]
        public class ProductInfo
        {
            public int ProductId { get; set; }
            public int CultureId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public decimal Sum { get; set; }
            public virtual Product Product { get; set; }
        }

    For more information about how to config relationship between entities 

    Please refer to the following links:

    http://www.entityframeworktutorial.net/entity-relationships.aspx

    https://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2015/10/21/how-to-create-relationships-between-entities-in-the-entity-framework-code-first-approach.aspx

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best regard

    Cathy

    Thursday, June 22, 2017 8:58 AM
  • User-821126599 posted

    this is what I got so far:

        public class Culture : Base
        {
            [ForeignKey("CategoryId")]
            public string Name { get; set; }
            public string Code { get; set; }
            public string CurrencySymbol { get; set; }
        }
        [Table("ShopCategory")]
        public class Category : ShopBase
        {
            public int CultureId { get; set; }
            public virtual Culture Culture { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public ICollection<Product> Products { get; set; } = new HashSet<Product>();
        }
        [Table("ShopProduct")]
        public class Product : ShopBase
        {
            public bool Active { get; set; } = true;
            public int CategoryId { get; set; }
            public virtual Category Category { get; set; }
            public ICollection<ProductInfo> InfoItems { get; set; } = new HashSet<ProductInfo>();
        }


    The problem is when I return a Json object, the categorys repeats the Products in the Category Products. But I need the Culture object from the Category object.

    Is this bad design?

    The idea was to access CultureInfo through ProductInfo -> Product -> Category. The culture object defines the language to display

    Many thanks in advance

    Friday, June 23, 2017 11:25 AM
  • User-821126599 posted

    Im I looking at this all wrong? The idea is that ShopCategory kan support different culture (English, Norwegian and so on). But since I cant filter include tables, how am I suppose to the get the correct ProductInfo which stores the CultureId?? PLEASE HELP! :)

    Sunday, June 25, 2017 11:14 PM