Asp.net mvc 3 facebook墙类型需求的数据库结构
我在一个社交网站上工作,它有一个类似facebook的墙,用户可以在那里发布状态、图片等。为用户建一堵墙并不难。我有这样的结构Asp.net mvc 3 facebook墙类型需求的数据库结构,asp.net-mvc-3,sql-server-2008,database-design,Asp.net Mvc 3,Sql Server 2008,Database Design,我在一个社交网站上工作,它有一个类似facebook的墙,用户可以在那里发布状态、图片等。为用户建一堵墙并不难。我有这样的结构 Wall { Id, ToUserId (BigInt) (Owner of wall) ByUserId (The person who is posting) Content (message, pic or something else) more fields.... } Wall {
Wall
{
Id,
ToUserId (BigInt) (Owner of wall)
ByUserId (The person who is posting)
Content (message, pic or something else)
more fields....
}
Wall
{
Id,
ObjectId (BigInt)(PageId/UserId/GroupId (one of them))
ByUserId (The person who is posting)
Content (message, pic or something else)
more fields....
}
这面墙只是为用户设计的,我不能重复使用它,例如,我想网站会有
其他对象也像页面、组等,所以所有对象都有墙。我不想为每面墙创建单独的表
然后我想,我可以用ObjectId代替TuserID,我的结构将是这样的
Wall
{
Id,
ToUserId (BigInt) (Owner of wall)
ByUserId (The person who is posting)
Content (message, pic or something else)
more fields....
}
Wall
{
Id,
ObjectId (BigInt)(PageId/UserId/GroupId (one of them))
ByUserId (The person who is posting)
Content (message, pic or something else)
more fields....
}
当我在表中使用增量字段时,页面可以具有与用户相同的Id。所以这又是一个问题
接下来,我想,我的对象应该是字符串类型
Wall
{
Id,
ObjectId (string(10))(PageId/UserId/GroupId (one of them))
ByUserId (The person who is posting)
Content (message, pic or something else)
more fields....
}
现在,对于一个用户,我将在数字后面加上“U”,这样对象Id将是“1U”、“2U”,对于页面,它们将是“1P”、“2P”,对于组,“1G”、“2G”
但我还是不服气,想听听专家对此的建议
更新
谢谢,伙计们,我的问题是,我怎么能为所有类型的墙保留一张桌子
A wall for a User
A Wall for a Page
A Wall for a Group
如果你看看facebook,它的一面墙或者至少是一面墙,我想建立它,所以它应该连接到任何对象(用户、页面、群组或任何东西)
我希望这现在更有意义。对于每个可能的引用,我会选择单独的可空字段。这还允许您使用外键,如果需要,您甚至可以进行检查约束,以便所有行都必须定义一个引用。我绝对不推荐使用字符串id的想法——在这方面进行连接是无效的
create table [Posts]
(
[ID] int not null identity(1, 1),
[ID_FromUser] int not null,
[ID_ToUser] int null,
[ID_ToPage] int null,
[ID_ToGroup] int null,
[Content] nvarchar(max) not null,
-- more stuff
constraint [FK_Posts_FromUser] foreign key ([ID_FromUser]) references [Users]([ID]),
constraint [FK_Posts_ToUser] foreign key ([ID_ToUser]) references [Users]([ID]),
constraint [FK_Posts_ToPage] foreign key ([ID_ToPage]) references [Pages]([ID]),
constraint [FK_Posts_ToGroup] foreign key ([ID_ToGroup]) references [Groups]([ID]),
constraint [PK_Posts] primary key ([ID])
)
对于每个可能的引用,我会选择单独的可空字段。这还允许您使用外键,如果需要,您甚至可以进行检查约束,以便所有行都必须定义一个引用。我绝对不推荐使用字符串id的想法——在这方面进行连接是无效的
create table [Posts]
(
[ID] int not null identity(1, 1),
[ID_FromUser] int not null,
[ID_ToUser] int null,
[ID_ToPage] int null,
[ID_ToGroup] int null,
[Content] nvarchar(max) not null,
-- more stuff
constraint [FK_Posts_FromUser] foreign key ([ID_FromUser]) references [Users]([ID]),
constraint [FK_Posts_ToUser] foreign key ([ID_ToUser]) references [Users]([ID]),
constraint [FK_Posts_ToPage] foreign key ([ID_ToPage]) references [Pages]([ID]),
constraint [FK_Posts_ToGroup] foreign key ([ID_ToGroup]) references [Groups]([ID]),
constraint [PK_Posts] primary key ([ID])
)
我不明白你为什么要创建某种“字符串”引用结构。这似乎不是“关系” 这个怎么样 用户有许多帖子 用户有一个墙 一个帖子有一个墙 一个帖子有一个用户 墙有许多帖子 墙有一个用户 POCO的(因为我懒得写SQL)
公共类用户
{
public int UserId{get;set;}
public ICollection Posts{get;set;}//此用户所写的帖子。可能是针对不同的墙的
公共墙{get;set;}//用户的墙。
}
公营职位
{
公共int PostId{get;set;}
公共用户User{get;set;}//写文章的用户。
公共墙{get;set;}//此帖子所属的墙。
}
公共课墙
{
公共int WallId{get;set;}
公共ICollection Posts{get;set;}//此墙上的所有帖子。
}
假设:
- 一个帖子只能针对一个墙李>
公共类用户
{
public int UserId{get;set;}
public ICollection Posts{get;set;}//此用户所写的帖子。可能是针对不同的墙的
公共墙{get;set;}//用户的墙。
}
公营职位
{
公共int PostId{get;set;}
公共用户User{get;set;}//写文章的用户。
公共墙{get;set;}//此帖子所属的墙。
}
公共课墙
{
公共int WallId{get;set;}
公共ICollection Posts{get;set;}//此墙上的所有帖子。
}
假设:
- 一个帖子只能针对一个墙李>