none
Restrict User pass Datetime column value for Save RRS feed

  • Question

  • We have WLF desktop application. We are using EF 5.0 Database first approach. We do not want DateTime column like 'CreatedDate' on each Table to be filled by user. At the time of record save, Database should manage it and pick the database server end. How to acheive this?
    • Edited by Dotnetavalanche Thursday, November 13, 2014 11:12 PM clarity added
    Thursday, November 13, 2014 11:01 PM

Answers

  • Hello DotnetIndia,

    >>We do not want DateTime column like 'CreatedDate' on each Table to be filled by user, the time of record save, Database should manage it and pick the database server end. How to acheive this?

    For this, I think we need to mark these datetime properties as private and these properties should return DateTime.Now as below:

    private Nullable<System.DateTime> CreateDate
    
    {
    
                get { return DateTime.Now; }
    
                set { ; }
    
    }
    

    I do not recommend to modify the code directly because these entities would be reset after updating the model next time. A proper way is to modify the .tt template file. Since you are using database first approach, you already know these names of these datetime columns, you could create a list<T> collection and add all datetime type columns to it and do a filte operation, the modified code should look like below:

    public string Property(EdmProperty edmProperty)
    
        {
    
                  List<string> DateColumns= new List<string>(){"CreateDate"};
    
                  if(DateColumns.Contains(_code.Escape(edmProperty)))
    
                  {
    
                         return @"private Nullable<System.DateTime> CreateDate
    
                                      {
    
                                             get { return DateTime.Now; }
    
                                             set { ; }
    
                                      }";
    
                  }
    
                  else
    
                  {
    
                         return string.Format(
    
                CultureInfo.InvariantCulture,
    
                "{0} {1} {2} {{ {3}get; {4}set; }}",
    
                Accessibility.ForProperty(edmProperty),
    
                _typeMapper.GetTypeName(edmProperty.TypeUsage),
    
                _code.Escape(edmProperty),
    
                _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
    
                _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
    
                  }
    
        }
    

    Open the XXX.tt file and find the “public string Property(EdmProperty edmProperty)” method and replace its code with above one. Of course, you need to add your datetime type columns to DateColumns.

    Please have a try and if it does not work for you, please feel free let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 14, 2014 2:13 AM
    Moderator
  • We have WLF desktop application. We are using EF 5.0 Database first approach. We do not want DateTime column like 'CreatedDate' on each Table to be filled by user. At the time of record save, Database should manage it and pick the database server end. How to acheive this?

    You can look into the Computed column in EF for Database First, which would be set in the EDMX for a given table column. It supposed to tell EF to not do anything with the table column, and it allows the DB to set a default value in the column like a datetime stamp.

    Friday, November 14, 2014 3:23 PM

All replies

  • Hello DotnetIndia,

    >>We do not want DateTime column like 'CreatedDate' on each Table to be filled by user, the time of record save, Database should manage it and pick the database server end. How to acheive this?

    For this, I think we need to mark these datetime properties as private and these properties should return DateTime.Now as below:

    private Nullable<System.DateTime> CreateDate
    
    {
    
                get { return DateTime.Now; }
    
                set { ; }
    
    }
    

    I do not recommend to modify the code directly because these entities would be reset after updating the model next time. A proper way is to modify the .tt template file. Since you are using database first approach, you already know these names of these datetime columns, you could create a list<T> collection and add all datetime type columns to it and do a filte operation, the modified code should look like below:

    public string Property(EdmProperty edmProperty)
    
        {
    
                  List<string> DateColumns= new List<string>(){"CreateDate"};
    
                  if(DateColumns.Contains(_code.Escape(edmProperty)))
    
                  {
    
                         return @"private Nullable<System.DateTime> CreateDate
    
                                      {
    
                                             get { return DateTime.Now; }
    
                                             set { ; }
    
                                      }";
    
                  }
    
                  else
    
                  {
    
                         return string.Format(
    
                CultureInfo.InvariantCulture,
    
                "{0} {1} {2} {{ {3}get; {4}set; }}",
    
                Accessibility.ForProperty(edmProperty),
    
                _typeMapper.GetTypeName(edmProperty.TypeUsage),
    
                _code.Escape(edmProperty),
    
                _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
    
                _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
    
                  }
    
        }
    

    Open the XXX.tt file and find the “public string Property(EdmProperty edmProperty)” method and replace its code with above one. Of course, you need to add your datetime type columns to DateColumns.

    Please have a try and if it does not work for you, please feel free let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 14, 2014 2:13 AM
    Moderator
  • We have WLF desktop application. We are using EF 5.0 Database first approach. We do not want DateTime column like 'CreatedDate' on each Table to be filled by user. At the time of record save, Database should manage it and pick the database server end. How to acheive this?

    You can look into the Computed column in EF for Database First, which would be set in the EDMX for a given table column. It supposed to tell EF to not do anything with the table column, and it allows the DB to set a default value in the column like a datetime stamp.

    Friday, November 14, 2014 3:23 PM
  • I will try this.
    Sunday, November 16, 2014 2:03 PM
  • Hello DotnetIndia,

    Any update? I have marked replies as answer since I think they are helpful, if you think they provide no help, please unmark it.

    Thank you for your understanding and support.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 25, 2014 8:25 AM
    Moderator