[英]How to structure the data model for profiles for user
In my application, a user can choose between 3 profiles to created. 在我的应用程序中,用户可以在3个配置文件中进行选择。
For now, I have 3 tables in the database representing the profiles. 现在,我在数据库中有3个表代表配置文件。 One table for each profile.
每个配置文件一张桌子。
But I don't know how to work with this In a proper way In the code. 但是我不知道如何在代码中以适当的方式使用它。
Let say that I visit a profile by entering the following URL: 假设我通过输入以下网址来访问个人资料:
mysite.com/fishness
Either to get data from the database that belongs to the "fishness" profile, I must search In three tables. 要从属于“ fishness”概要文件的数据库中获取数据,我都必须在三个表中进行搜索。 How should I do the search?
我应该如何搜寻? Should I just do three querys for each table?
我应该为每个表做三个查询吗? Or Is It a nicer way to do this?
还是这是一种更好的方法?
Another thing Is when the user logs In. 另一件事是用户登录时。 How should I get the profile that belongs to the user who logs in?
如何获取属于登录用户的个人资料? Should I have navigation properties In my
AppUser
model( the model for the user )? 我应该在
AppUser
模型(用户模型)中具有导航属性吗?
The ugly thing In my opinion about this, Is that I must then add navigation
properties for all three profiles In my AppUser
. 在我看来,这很丑陋,那就是我必须在
AppUser
为所有三个配置文件添加navigation
属性。
Can anyone give me tips about how to design this? 谁能给我有关如何设计的提示?
Here Is my three profile tables: 这是我的三个配置文件表:
Profile1: 资料1:
Id
UserId (the users Id)
Name
CoverPicture
Description
WelcomePage
Profile2: Profile2:
Id
UserId (the users Id)
Name
CoverPicture
Description
WelcomePage
Profile3: Profile3:
Id
UserId
Firstname
Lastname
Birth
Profilepicture
WelcomePage
You design is not good because it is not scallable , imagine you want to create a new profile , you will be obliged to create new table and edit your code . 您的设计不好,因为它不是可调用的,假设您想创建一个新的配置文件,则必须创建新表并编辑代码。
My suggestion is to create a table profile
and user
, user has a column profileId that reference profile column id (foreign key) . 我的建议是创建一个表
profile
和一个user
,用户有一个引用概要文件列ID(外键)的列profileId 。
Table profile
ProfileID
ProfileDesignation
Table user
UserId
Firstname
Lastname
Birth
Profilepicture
CoverPicture
Description
WelcomePage
ProfileID references profile.ProfileID
Your question appears to be a classic case of "how do I implement inheritance in my database". 您的问题似乎是“如何在数据库中实现继承”的经典案例。 There are several similar questions on SO;
关于SO有几个类似的问题。 the best answer is here .
最好的答案在这里 。
As far as I understand, you have 3 types of profile. 据我了解,您有3种类型的个人资料。 They each share some attributes (ID, UserID and WelcomePage), but each type may have attributes that are not shared (profile 1 and profile 2 look the same to me).
它们每个都共享一些属性(ID,UserID和WelcomePage),但是每种类型都可能具有不共享的属性(配置文件1和配置文件2在我看来是相同的)。 That's a classic inheritance scenario.
这是经典的继承方案。
There is no clean implementation in the relational model for this situation; 在这种情况下,关系模型中没有干净的实现; instead, you must make trade-offs.
相反,您必须权衡取舍。 Your current design is "table per concrete", and is perfectly reasonable.
您当前的设计是“每张桌子具体”,并且绝对合理。
I created two sample table AppUser and UserProfile.Both tables are linked with foreign key AppuserId which is Primary key for Appuser. 我创建了两个示例表AppUser和UserProfile。两个表都与外键AppuserId链接,后者是Appuser的主键。
If you want to create model for user Profile you can create separate classes per each table.when user login based on AppuserId you can access profile information. 如果要为用户配置文件创建模型,则可以为每个表创建单独的类。基于AppuserId的用户登录时,可以访问配置文件信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.