I work on an Entity Framework 6.1.3, ASP.NET MVC 5, Code First with an existing databse project. My database (SQL Server 2016) includes a table with a “ModifiedDate” (datetime, DEFAULT (getdate)) column. When trying to create a new record in browser the server applies the value automatically as it should be done.
Unfortunately, when I update an existing record the old ModifiedDate value as it is shown in the textbox is applied (not the current date). Moreover, the ModifiedDate textbox should always be inactive to the user, both when creating or editing a record (i.e. the user shouldn’t be allowed to enter a value himself).
This is the model class for the “Title” table:
- public partial class Title
- {
- public int TitleID { get; set; }
- [Column("Title")]
- [Required]
- [StringLength(50)]
- public string Title1 { get; set; }
- [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
- public DateTime? ModifiedDate { get; set; }
- }
Any suggestions would be appreciated.
Darfer

Nitin SontakkeRank #264 · 9.6K reputationPosted Sep 27, 2016, 12:47 AM EDT
Darfer27 reputationPosted Sep 27, 2016, 3:29 AM EDT
Darfer.
Nitin SontakkeRank #264 · 9.6K reputationPosted Sep 27, 2016, 3:42 AM EDT
Darfer27 reputationPosted Sep 27, 2016, 8:58 PM EDT
Darfer
Nitin SontakkeRank #264 · 9.6K reputationPosted Sep 27, 2016, 9:12 PM EDT
Darfer27 reputationPosted Sep 28, 2016, 10:25 PM EDT
Darfer27 reputationPosted Sep 29, 2016, 12:36 AM EDT
1. I've set the ModifiedDate property as:
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime ModifiedDate { get; set; }
ON [dbo].[Title]
AFTER UPDATE
AS
UPDATE dbo.Title
SET ModifiedDate = GETDATE()
WHERE TitleID IN (SELECT DISTINCT TitleID FROM Inserted)
Thanks everybody for assisting me.
Darfer27 reputationPosted Oct 4, 2016, 12:29 PM EDT
What I meant by "not working" is that still, it was fine for creating a new record, but in edit mode asking for user input for the date or apply the date that the record was created. I've hidden the textbox for ModifiedDate in Create and Edit views so that ModifiedDate only shows up in List view.
Nitin SontakkeRank #264 · 9.6K reputationPosted Oct 4, 2016, 7:16 PM EDT
Darfer27 reputationPosted Oct 10, 2016, 10:56 AM EDT
Midhun TpRank #388 · 6.5K reputationPosted Sep 26, 2016, 10:50 PM EDT
Darfer27 reputationPosted Sep 26, 2016, 11:42 PM EDT
Midhun TpRank #388 · 6.5K reputationPosted Sep 27, 2016, 12:26 AM EDT
D DonthuRank #2,299 · 614 reputationPosted Sep 29, 2016, 1:11 AM EDT
Nitin SontakkeRank #264 · 9.6K reputationPosted Sep 29, 2016, 1:37 AM EDT
Omid Naghizadeh4 reputationPosted Apr 17, 2021, 2:31 PM EDT