Pergunta

I'm still new in creating Models using Entity Framework and MVC 4 Razor. I'm having a problem on how can I save a history of a model. How can I create a model that have a history on specific tables or fields ? For ex: If I wish to create a history on the changes on the school. Its still not clear to me how will I I create the model that saves history. How will be the triggering do I have to execute the save function on different models with the same data ?

Thank you so much in advance.

If anyone could be a simple example of model and a model history and how it is functioning, I'll be very grateful. Like a Sales or sales history.

Here's my code


One To Many public class Child { [Key] public int ChildID { get; set; }

    [Required,Display(Name="Project Code")]
    public string ProjectCode { get; set; }

    public string Status { get; set; }

    [DataType(DataType.Date)]
    public DateTime StatusDate { get; set; }

    public string FamilyName { get; set; }
    public string GivenName { get; set; }
    public string MiddleName { get; set; }

    [DataType(DataType.Date)]
    public DateTime Birthdate { get; set; }

    public string Gender {get;set;}
    public string Address { get; set; }
    public string Section { get; set; }
    public int SchoolLevelID { get; set; }
    public int SchoolYearID { get; set; }
    public int AreaID { get; set; }
    public int SchoolID { get; set; }
    public int GradeLevelID { get; set; }

    //Foreign Key - One to Many 
    public virtual SchoolLevel SchoolLevel { get; set; }
    public virtual SchoolYear SchoolYear { get; set; }
    public virtual Area Area { get; set; }
    public virtual School School { get; set; }
    public virtual GradeLevel GradeLevel{get;set;}

    //Child is foreign key at the table
    public virtual ICollection<Guardian> Guardians { get; set; }

}


public class SchoolLevel
{
    public int SchoolLevelID { get; set; }
    public string SchoolLevelName { get; set; }

    public virtual ICollection<Child> Children { get; set; }
}

public class SchoolYear
{
    public int SchoolYearID { get; set; }
    public string SchoolYearName { get; set; }

    public virtual ICollection<Child> Children{get;set;}
}

public class Area
{
    public int AreaID{get;set;}
    public string AreaName { get; set; }

    public virtual ICollection<Child> Children{get;set;}
}

public class School
{
    public int SchoolID { get; set; }
    public string SchoolName{get;set;}

    public virtual ICollection<Child> Children { get; set; }
}
public class GradeLevel
{
    public int GradeLevelID{get;set;}
    public string GradeLevelName { get; set; }

    public virtual ICollection<Child> Children { get; set; }
}


public class ChildDbContext : DbContext
{
    public DbSet<Child> Children { get; set; }
    public DbSet<SchoolLevel> SchoolLevels { get; set; }
    public DbSet<SchoolYear> SchoolYears { get; set; }
    public DbSet<Area> Areas { get; set; }
    public DbSet<School> Schools { get; set; }
    public DbSet<GradeLevel> GradeLevels { get; set; }
    public DbSet<Guardian> Guardians { get; set; }
}
Foi útil?

Solução

You can use this approach: Create a History model. That contains 1 changeness like o log.

public class History 
{
    public int HistoryId { get; set; }

    public int ModelType { get; set; }  //it is ModelTypeEnum value.
    public int ModelId { get; set; }
    public string PropertyName { get; set; }
    public string Propertyvalue {get;set;}
    public DateTime ChangeDate { get; set; }

    public int ChangedUserId { get; set; }
}

And Enum:

public enum ModelTypeEnum
{
    Child =1,
    SchoolLevel = 2,
    //etc.. 
};

For example, when you edit 1 Child entity, give changed properties name and value, it's id, type and others (ChangeDate, ChangedUserId) to History and save histories. If 3 properties will change you should save 3 history entities. Then, you can load (filter) histories by ModelId, by ChangedUserId etc.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top