显示名称 - “用户” + UserId - 执行此操作的最佳方法
我想做一些像对 DisplayName 所做的那样。当有人没有输入 DisplayName 时,我想将其默认为“User”+ UserId。因此,第一个注册的用户将获得 User1
- 因为 UserId 将是 1,第二个 User2
- 因为 UserId 将是 2,依此类推。
我能想到的最简单的方法是使用触发器,但我不喜欢触发器。我可以保存用户,然后在保存后更新用户(基本上是一个触发器,但在我的代码中完成)。关于如何处理这个问题还有其他想法吗?有没有办法可以设置数据库中列的默认值?
我正在使用 Asp.Net MVC 2、C# 4、EF4 和 SQL Server 2008。
I want to do something like SO does with DisplayName. When someone does not enter a DisplayName, I want to default this to 'User' + UserId. So the first user who signs up would get User1
- since UserId will be 1, the second User2
- since UserId will be 2, and so on.
The easiest way I can think of doing this is using a trigger, but I don't like triggers. I could save the user, then update the user after the save (basically a trigger, but done in my code). Any other ideas on how I can handle this? Is there a way I can set the default value on the column in the database?
I am using Asp.Net MVC 2, C# 4, EF4, and SQL Server 2008.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以有一个计算列
You can have a computedColumn
我倾向于在保存到数据库之前生成它。没有输入 = 编一个。
我也不会有用户 User1、User2,...,而是 user+random:user6238945、user9287561、user8934678 等
如果您使用 1、2、3、4 等,那么您就会暴露内部序列。如果有人得到“user4”,那么我就知道存在其他用户 1 到 3,以及数字 5。AOL 或 Compuserve(?)多年来一直利用此漏洞进行网络钓鱼攻击。
I would be inclined to generate it before saving to the database. No input = make one up.
I also would not have user User1, User2, ... but user+random: user6238945, user9287561, user8934678, etc
If you use 1, 2, 3, 4 etc then you are exposing an internal sequence. If someone gets "user4" then I know other users 1 to 3 exist, as well as number 5. This exposure was used many years by AOL or Compuserve (?) for phishing attacks.
您可以在存储过程中设置用户名。如果用户名已传递给存储过程,只需保存它即可。否则,获取最后输入的用户的主键 ID 并加 1。将其格式化为您喜欢的任何格式(例如用户+新ID)并将其保存为新用户的用户名。
You can set the user name in the stored procedure. if a user name has been passed to stored procedure, simply save it. otherwise, get the primary key id of the last entered user and add 1 to it. format it to whatever you like (e.g. User + newID) and save this as the new user's user name.