如何为用户配置文件构建数据模型

How to structure the data model for profiles for user

在我的应用程序中,用户可以选择创建 3 个配置文件。

目前,我在数据库中有 3 个 table 代表配置文件。每个配置文件一个 table。

但我不知道如何在代码中以正确的方式使用它。

假设我通过输入以下 URL 来访问个人资料:

mysite.com/fishness

要从属于 "fishness" 配置文件的数据库中获取数据,我必须在三个 table 中搜索。我应该如何进行搜索? 我应该为每个 table 做三个查询吗?或者这是更好的方法吗?

另一件事是用户登录时。我应该如何获取属于登录用户的配置文件? 我的 AppUser 模型(用户模型)中是否应该有导航属性?

丑陋的事情在我看来,我必须在我的 AppUser.

中为所有三个配置文件添加 navigation 属性

谁能给我一些设计技巧?

这是我的三个个人资料 tables:

个人资料 1:

Id
UserId (the users Id)
Name
CoverPicture
Description
WelcomePage

个人资料2:

Id
UserId (the users Id)
Name
CoverPicture
Description
WelcomePage

个人资料3:

Id
UserId
Firstname
Lastname
Birth
Profilepicture
WelcomePage

你的设计不好,因为它不可缩放,假设你想创建一个新的配置文件,你将不得不创建新的 table 并编辑你的代码。

我的建议是创建一个 table profileuser ,用户有一列 profileId 引用 配置文件 列 ID(外键).

Table profile
ProfileID 
ProfileDesignation

Table user
UserId
Firstname
Lastname
Birth
Profilepicture
CoverPicture
Description
WelcomePage
ProfileID references profile.ProfileID

您的问题似乎是 "how do I implement inheritance in my database" 的经典案例。 SO上有几个类似的问题;最佳答案是 here.

据我了解,您有 3 种类型的个人资料。它们各自共享一些属性(ID、UserID 和 WelcomePage),但每种类型可能具有不共享的属性(配置文件 1 和配置文件 2 在我看来是一样的)。这是一个经典的继承场景。

对于这种情况,关系模型中没有干净的实现;相反,您必须做出权衡。你现在的设计是"table per concrete",完全合理

我创建了两个示例 table AppUser 和 UserProfile.Both tables 与外键 AppuserId 链接,这是 Appuser 的主键。

如果您想为用户个人资料创建模型,您可以根据 AppuserId 为每个 table.when 用户登录创建单独的 类,您可以访问个人资料信息。