[英]How can I model this class in a database?
I need a little of help.我需要一点帮助。 This is my design to organize several categories.
这是我的设计来组织几个类别。
Category 1
Sub Category 1.1
Sub Category 1.1.1
Sub Category 1.2
Sub Category 1.3
It would be a collection.这将是一个集合。 I want to store it in a database, but I don't know how can I model it in a database table.
我想将它存储在数据库中,但我不知道如何将它 model 存储在数据库表中。 I'm using SQL Server CE.
我正在使用 SQL 服务器 CE。
UPDATE :更新:
I forgot put the objective number in the class (1.1, 1.1.1).我忘记在 class (1.1, 1.1.1) 中输入目标编号。
You'd have a table something like this:你会有一个像这样的表:
Then, if a category has a parent, you reference the id
of that other row.然后,如果一个类别有父类别,则引用该另一行的
id
。 So the table is self-referential.所以这个表是自引用的。 Toplevel categories have a null
parent_category_id
.顶级类别有一个 null
parent_category_id
。
When building tables like this you do need to be careful that you don't create a circular reference.在构建这样的表时,您需要注意不要创建循环引用。
For this, you can have a table where an item can reference its parent, if any.为此,您可以有一个表,其中项目可以引用其父项(如果有)。
If the ParentId
column is NULL
, the category is a root one.如果
ParentId
列是NULL
,则类别是根类别。 If not, the parent is referenced.如果不是,则引用父级。
You can then find the subcategories of a category by walking through the table and searching for items with ParentId
equal to Id
of the category.然后,您可以通过遍历表并搜索
ParentId
等于类别Id
的项目来查找类别的子类别。
Note that ParentId
must be indexed for better performance, and that there must be a foreign key of ParentId
to Id
to ensure the validity of your data.注意
ParentId
必须被索引以获得更好的性能,并且必须有ParentId
到Id
的外键以确保您的数据的有效性。
Storing recursively the categories:递归存储类别:
private void SaveCategoryRecursively(Category category)
{
foreach (var subCategory in category.SubCategories)
{
query(@"
insert into [dbo].[Categories] ([Id], [ParentId], ...)
values (@id, @parentId, ...)", ...);
this.SaveCategoryRecursively(subCategory);
}
}
public void SaveCategories(IEnumerable<Category> rootCategories)
{
foreach (var category in rootCategories)
{
query(@"
insert into [dbo].[Categories] ([Id], [ParentId], ...)
values (@id, NULL, ...)", ...);
this.SaveCategoryRecursively(category);
}
}
I'd use a simple Recursive Relation .我会使用一个简单的Recursive Relation 。 Each Category should have a unique ID (primary key) and an optional field specifying its parent, which would be a foreign key mapping back to the same table.
每个类别都应该有一个唯一的 ID(主键)和一个指定其父级的可选字段,这将是一个映射回同一个表的外键。 Categories with a
NULL
parent are top-level categories.具有
NULL
父级的类别是顶级类别。
The page I linked to also has information on how you can query this structure to find top-level or mid-level Categories.我链接到的页面还包含有关如何查询此结构以查找顶级或中级类别的信息。
Here is my recommendation.这是我的建议。 Create Three tables.
创建三个表。 I am assuming that each table has different columns
我假设每个表都有不同的列
Category { Id, Name,...}
SubCategory {Id, Name, ..., **ParentID**} [ParentID is a FK from Category table Id Column]
SubSubCategory {Id, Name, ..., **SubParentID**} [SubParentID is a FK from SubCategory table ParentID Column]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.