I’m having a problem with a C# MVC project in ASP.NET.
The problem I have is with a category system: If one category has been added to a company, then you should not be able to add any category that is a parent (higher level in the hierarchy) than what’s already been added (since it’s already included in the search).
Now I have added this in the C# code already using this:
if (companyToUpdate.Category.Any(x => x.Parent.Any(z => z.ID == currentCategoryID))) {
//already exists
}
Now the problem is that I would like to add this check as a stored procedure in the SQL Server database.
My structure looks like this:
Company (all companies)
ID Title
1 StackOverflow
Category (all categories)
ID Title
1 Website
2 Helpful site
3 Very helpful site
CategoryDetails (used to know parent/child of each category,
every category has a relationship to all of its parents)
(Website -> Helpful site -> Very helpful site)
Parent_ID Child_ID
1 2
1 3
2 3
CompanyCategoryDetails (what categories have been added to this company?)
Company_ID Category_ID
1 3
Now to the problem:
When you are trying to add a new line (1, 2) into CompanyCategoryDetails, it should be denied, since because you have already added Category_ID 3 to it, that means Category_ID 2 is already included which specified in the CategoryDetails table.
I started doing some check where I INNERJOIN the tables, but didnt get far before I realized it couldnt be done the way I was thinking.
I think the check you are asking for is this:
This will check for trying to add to a compnay (a) a parent category or (b) an existing category