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
Omid NaghizadehPosted Apr 17, 2021, 6:31 PM
DarferPosted Oct 10, 2016, 2:56 PM
Nitin SontakkePosted Oct 4, 2016, 11:16 PM
DarferPosted Oct 4, 2016, 4:29 PM
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 SontakkePosted Sep 29, 2016, 5:37 AM
D DonthuPosted Sep 29, 2016, 5:11 AM
DarferPosted Sep 29, 2016, 4:36 AM
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.
DarferPosted Sep 29, 2016, 2:25 AM
Nitin SontakkePosted Sep 28, 2016, 1:12 AM
DarferPosted Sep 28, 2016, 12:58 AM
Darfer
Nitin SontakkePosted Sep 27, 2016, 7:42 AM
DarferPosted Sep 27, 2016, 7:29 AM
Darfer.
Nitin SontakkePosted Sep 27, 2016, 4:47 AM
Midhun TpPosted Sep 27, 2016, 4:26 AM
DarferPosted Sep 27, 2016, 3:42 AM
Midhun TpPosted Sep 27, 2016, 2:50 AM