Answered by:
how to set null value to datetimepicker and textbox fields to save data into EF

Question
-
Hi All.
In Save button event I have
private void SaveEmployee_Click(object sender, RoutedEventArgs e) { var employee = new EmployeeDT(); if (Validate == true) { employee.EmployeeId = Guid.NewGuid(); employee.Name = NameTextBox.Text; employee.Department = DepartmentTextBox.Text; employee.IsActive = IsActiveCheckBox.IsChecked.HasValue ? IsActiveCheckBox.IsChecked.Value : false; employee.CreatedDate = DateTime.Now; employee.CreatedBy = CreatedByTextBox.Text;
employee.UpdateDate = ???????
employee.UpdateBy = ??????? var ea = new EmployeeAdapter(); ea.SaveEmployee(employee); } else errorMessage(); }
How assign to employee.UpdateDate and employee.UpdateBy null or DBNull value?Thanks.
- Edited by zleug Monday, August 17, 2020 7:03 PM
Monday, August 17, 2020 7:02 PM
Answers
-
Hello,
Is there are reason you would set these values to null? It would seem prudent to override SaveChanges and set those values there.
Example done in a dbContext class.
public override int SaveChanges() { ChangeTracker.DetectChanges(); foreach (var entry in ChangeTracker.Entries()) { if (entry.State == EntityState.Added || entry.State == EntityState.Modified) { entry.Property("LastUpdated").CurrentValue = DateTime.Now; entry.Property("LastUser").CurrentValue = Environment.UserName; if (entry.Entity is Contact1 && entry.State == EntityState.Added) { entry.Property("CreatedAt").CurrentValue = DateTime.Now; entry.Property("CreatedBy").CurrentValue = Environment.UserName; } } else if (entry.State == EntityState.Deleted) { // Change state to modified and set delete flag entry.State = EntityState.Modified; entry.Property("isDeleted").CurrentValue = true; } } return base.SaveChanges(); }
See more here on shadow properties. For EF-6 see the following post.
Bottom line is you should not be setting these properties to null but if you must see the following post.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Proposed as answer by Timon YangMicrosoft contingent staff Tuesday, August 18, 2020 8:08 AM
- Marked as answer by zleug Thursday, August 20, 2020 12:04 PM
Monday, August 17, 2020 7:19 PM
All replies
-
Hello,
Is there are reason you would set these values to null? It would seem prudent to override SaveChanges and set those values there.
Example done in a dbContext class.
public override int SaveChanges() { ChangeTracker.DetectChanges(); foreach (var entry in ChangeTracker.Entries()) { if (entry.State == EntityState.Added || entry.State == EntityState.Modified) { entry.Property("LastUpdated").CurrentValue = DateTime.Now; entry.Property("LastUser").CurrentValue = Environment.UserName; if (entry.Entity is Contact1 && entry.State == EntityState.Added) { entry.Property("CreatedAt").CurrentValue = DateTime.Now; entry.Property("CreatedBy").CurrentValue = Environment.UserName; } } else if (entry.State == EntityState.Deleted) { // Change state to modified and set delete flag entry.State = EntityState.Modified; entry.Property("isDeleted").CurrentValue = true; } } return base.SaveChanges(); }
See more here on shadow properties. For EF-6 see the following post.
Bottom line is you should not be setting these properties to null but if you must see the following post.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Proposed as answer by Timon YangMicrosoft contingent staff Tuesday, August 18, 2020 8:08 AM
- Marked as answer by zleug Thursday, August 20, 2020 12:04 PM
Monday, August 17, 2020 7:19 PM -
Hi zleug,
Thank you for posting here.
If you need to set it to null for some reason, you can also take a look at this document.
Nullable value types (C# reference)
DateTime? datetime = null;
Best Regards,
Timon
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Tuesday, August 18, 2020 8:14 AM