简体   繁体   English

如何在数据库中使用 model 这个 class?

[英]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:你会有一个像这样的表:

  • category类别
    • id (primary key, not null) id(主键,不为空)
    • name (text, not null)名称(文本,不为空)
    • parent_category_id (foreign key to category.id, nullable) parent_category_id(category.id 的外键,可为空)

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必须被索引以获得更好的性能,并且必须有ParentIdId的外键以确保您的数据的有效性。

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.

相关问题 如何显示来自数据库的模型属性名称? - How can I display a model property name that is coming from the database? 如何在不删除数据库的情况下更改模型? - How can I change the model without having to delete the database? 如何使用WPF和Linq-to-entities模型搜索数据库 - How can I search into database with WPF and Linq-to-entities model Model Class DisplayFormat()我如何本地化NullDisplayText? - Model Class DisplayFormat() How can I localization NullDisplayText? 如何使用我的 model class 序列化这个 Json? - How can I serialize this Json using my model class? 如何在 model class 中将两个数据放入字符串中? - How can I put two data in string in model class? 如何在.CS类中使用Model.NodeById(1234)? - How can I use Model.NodeById(1234) in a .CS Class? 如何根据某些条件向模型类添加属性 - How Can I Add Attributes To Model Class Based On Some Conditions 在查询XDocument时如何使用类作为数据模型? - How can I use a class as a data model in querying XDocument? 在执行“从数据库更新 Model”时,如何防止我的 model 文件被刷新? - How can I keep my model files from being refreshed when doing an "Update Model from Database"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM