在asp.net mvc 5中插入创建日期(现在)和创建者(用户名) [英] Insert created date(now) and created by(username) in asp.net mvc 5

查看:160
本文介绍了在asp.net mvc 5中插入创建日期(现在)和创建者(用户名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人帮我在asp.net5中插入创建日期(现在)和创建者(用户名). 每当我在crud应用程序中创建新记录时,以上记录都应自动输入数据库中.

Please anybody help me to Insert created date(now) and created by(username) in asp.net5. whenever I create the new record in crud application the above records should be entered automatically in database.

问候阿伦·萨哈尼(arun sahani)

regards arun sahani

推荐答案

与MVC或任何技术无关.您将必须通过UI传递这些值,或使用SQL默认值进行填充

It is nothing to do with MVC or any technology. You will have to either pass these values from UI or use SQL Default values to populate

CreateUserId nvarchar(55) NOT NULL CONSTRAINT [DF_Table_CreateUserId] DEFAULT suser_sname()

仅在使用登录的用户凭据从UI到DB执行操作的情况下,使用suser_sname.在大多数Web应用程序中,并不是这样,因此您可能最终将实际的名称或用户ID传递给数据库,就像为该表传递的任何其他字段一样.

Use suser_sname only in those cases where you perform your operations from UI to DB with the logged-in user credentials. In most of the web apps, it is not the way, so you may end up passing the actual your name or your user id to database like any other fields you pass for that table.

对于时间戳,您可以公平地使用此默认值

For timestamp, you can fairly use this Default value

CreateDateTime DateTime NOT NULL DEFAULT GETDATE()

每当您在表中插入值时,这将自动填充创建的时间戳.插入时,您无需为此列传递任何值.如果要从UI插入其他值,则可以选择传递值.

This will automatically populate created time stamp whenever you insert values into that table. You don't need to pass any value for this column while inserting. You can optionally pass value, if you want to insert some other value from UI.

例如,让我们说,您没有在字段中设置DEFAULT值;您当然可以从UI传递值来填充这些字段;如果有人不通过UI流程将一行直接插入表中会发生什么情况?可能是管理员插入一行?您需要考虑所有这些情况.

Let us say for example, you don't set DEFAULT values in fields; you can certainly pass values from UI to populate those fields; What happens when someone inserts a row directly into table without going through UI Process? May be a an Admin inserting a row? You need to consider all those scenarios.

这篇关于在asp.net mvc 5中插入创建日期(现在)和创建者(用户名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆