Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
CREATE TABLE [dbo].[LoginLevel_TBL](
	[LoginLevel_ID] [int] IDENTITY(1,1) NOT NULL,
	[LoginLevel_Name] [varchar](500) NULL,
	[Create_Date] [datetime] NULL,
	[Update_Date] [datetime] NULL,
	[Delete_Date] [datetime] NULL,
	[Delete_Status] [bit] NULL,
	[Login_ID] [int] NULL,
 CONSTRAINT [PK_LoginLevel_TBL] PRIMARY KEY CLUSTERED 
(
	[LoginLevel_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


This is my table where I create in sql server 2012 and connect with ado entity framework for c# application. For doing soft delete and track creation , updation I have tried a step create base entity and override dbcontext it work but when add a new table everything reset.

I face 2 issues when i am using the below code
It reset when i update entity frame work when i add a new table all the code i wrote is reset
Next one is when i update the created date also be modified,
when i delete by mofiy the below there is no possibilty the row is removed from DB

Please anyone help me i was stuck on this


What I have tried:

C#
public class BaseEntity
  {
      public Nullable<System.DateTime> Create_Date { get; set; }
      public Nullable<System.DateTime> Update_Date { get; set; }
      public Nullable<System.DateTime> Delete_Date { get; set; }
      public Nullable<bool> Delete_Status { get; set; }
      public Nullable<int> Login_ID { get; set; }

  }

create above class and mofify the below file create by entity
C#
 public class LoginLevel_TBL: BaseEntity
 public partial class SchoolDBEntities : DbContext
  public override int SaveChanges()
   {
       AddTimestamps();
       return base.SaveChanges();
   }
private void AddTimestamps()
   {
       var entities = ChangeTracker.Entries().Where(x => x.Entity is BaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));

       var currentUsername = !string.IsNullOrEmpty(System.Web.HttpContext.Current?.User?.Identity?.Name)
           ? HttpContext.Current.User.Identity.Name
           : "Anonymous";

       foreach (var entity in entities)
       {
           if (entity.State == EntityState.Added)
           {
               ((BaseEntity)entity.Entity).DateCreated = DateTime.UtcNow;
               ((BaseEntity)entity.Entity).UserCreated = currentUsername;
           }

           ((BaseEntity)entity.Entity).DateModified = DateTime.UtcNow;
           ((BaseEntity)entity.Entity).UserModified = currentUsername;
       }
   }


I face 2 issues when i am using the below code
It reset when i update entity frame work when i add a new table all the code i wrote is reset
Next one is when i update the created date also be modified,
when i delete by mofiy the below there is no possibilty the row is removed from DB


Please anyone help me i was stuck on this
Posted
Updated 26-Nov-17 6:17am
v2

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