Click here to Skip to main content
15,844,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
My data model (Entity Framework 6.1.3, ASP.NET MVC 5, Code First with an existing database, SQL Server 2016) includes a “Title” table with a “ModifiedDate” (datetime, DEFAULT (getdate)) column.

On creating a new record in browser the user is asked to type the ModifiedDate value instead of SQL Server applying them automatically. I tried a few tricks I could think of (I even created an AFTER UPDATE trigger in the table to set the date) but without success. After removing ModifiedDate column from the model and creating new controller and views the values are applied by SQL Server but I cannot longer see the values anymore.

By reinserting the ModifiedDate column and application of the [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribute I’m then able to create a new record but when editing an existing record a new value for ModifiedDate is not generated and only the value that shows in the ModifiedDate textbox is applied (that is the date the record has been created). I wonder about the way to handle this situation. I just wish the server to apply the ModifiedDate value and being able to see this value.

Here’s the models code:


[Table("Title")]
public partial class Title
{
    public int TitleID { get; set; }

    [Required]
    [StringLength(50)]
    public string Title { get; set; }

    public DateTime ModifiedDate { get; set; }
}


Thanks in advance, Corbex.

What I have tried:

I have tried this model instead:
[Table("Title")]
public partial class Title
{
    public int TitleID { get; set; }

    [Required]
    [StringLength(50)]
    public string Title { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime ModifiedDate { get; set; }
}
Posted
Updated 22-Sep-16 7:23am
v3

1 solution

 
Share this answer
 
Comments
Corbex 23-Sep-16 0:26am    
Hi sixthplanets. Thanks for your responce. I'm afraid this is a bit complicated for me as I'm just begining C#/MVC/Entity Framework. I already have a datebase and I try to create a UI to it. I don't need all tables to include modified date (datetime) columns. Any simpler solution to this problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900