EF database categories need suggestions

all and thanks for your help as i got a lot from you.

Now I have problems with understanding C#, MVC4 (EF: code first) and especial creating model for making non linear model: the task is to make categories/subcategories from external source, where i don't know the structure off categories and subcategories and what sub's level is. Its like category tree and at the end lets say its products. Right now I made paper work, and don't know how to code this.:

In my opinion there should be enough two model classes: 1.) Category 2.) Product

1. like this.



public class categories
 {
   double sub { get; set; }
   tring name { get; set; }
   link { get; set; }
   int colum { get; set; }
 }

2.

public class products {    
string ManufacturerName { get; set; }
        double where_iam_from { get; set; }
        string ManufacturerPartNumber { get; set; }
        string Description { get; set; }
}

In categories as i think there should be 2 numbers in field sub.. lets say for firs category from 10-99, if there is subcategory it becomes 1010, or 1110, and so till subcategories end. managed by code, and i will know full tree from this number in products. And product need to have this end value: where_iam_from, category tree, to find all way back.

And I don't know how to associate model classes; a big newbie.

I'm not sure if if i write understandable, if need more details i will sure add them.

Thank you all.

Edit: to make a bit clear: What i think i need in database is lines off products, and categories. And I want to use same class for categories, and sub-categories, and one for products. All the reference should be only 'where_iam_from', eg number. By this number doing math i should get full category list. example:

Category1 := 10;
  Category1_sub1:=1010;
  Category1_sub2:=1011;
    Category1_sub2_sub1:=101110;
Category2:= 11;
  Category2_sub1:=1110;

and so on for categories. And for products only reference to last sub number in categories lets say 101112101530; so by number length i can get sub level and with divide get full categories. Can i do that? Thanks.

Answers


I am not sure i understood the question correctly. But if you want to divide products into categories you could use something like this for EF code first.

public class Category
{
    [Key]
    public int Id { get; set; }
    public string CategoryName { get; set; }
    public virtual Category ParentCategory { get; set; }
    public virtual ICollection<Category> ChildCategories { get; set; }
    public virtual ICollection<Product> Products { get; set; }

    public Category()
    {
        this.ChildCategories = new HashSet<Category>();
        this.Products = new HashSet<Product>();
    }

}

public class Product
{
    [Key]
    public int Id { get; set; }
    public string ProductName { get; set; }
    public Category Category { get; set; }
}

// The database (DbContext) class

public class MyDb : DbContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

public class Application
{

    public Application()
    {
        MyDb db = new MyDb();
        Category categoryOne = new Category { CategoryName = "Category One" };ยจ
        Product productOne = new Product { ProductName = "Product One" };
        productOne.Category = categoryOne;
        db.Products.Add(productOne);
        db.SaveChanges();

        var productInCategory = db.Category.Where(x => x.CategoryName == "Category One").Include("Products").Products.ToList();

    }

}

I hope it can move you forward in your project. A good start is to read http://msdn.microsoft.com/en-us/data/jj193542.

Regards, Peter


Need Your Help

Tracing and diagnostics for System.Data.SQLConnection

.net sql debugging

Hi i was wondering if there is away to allow tracing / diagnostics on a SQLConnection via the app.config file much like you can with WCF when you need tracing support (as below)?

Javascripts stop working on heroku but work fine locally

jquery jquery-ui heroku ruby-on-rails-3.2 asset-pipeline

When I precompile locally everything works, but when I push to heroku (Cedar) the javascript stops working.