如何在MVC 4上创建特定模型的历史模型

时间:2014-03-28 04:02:55

标签: asp.net-mvc-4 model entity

我仍然是使用Entity Framework和MVC 4 Razor创建模型的新手。我遇到了如何保存模型历史的问题。如何创建在特定表或字段上具有历史记录的模型?例如:如果我想创建关于学校变化的历史。我还不清楚如何创建保存历史的模型。如何触发我必须在具有相同数据的不同模型上执行保存功能?

提前非常感谢你。

如果有人可以成为模型和模型历史的简单示例及其运作方式,我将非常感激。像销售或销售历史记录。

这是我的代码


一对多     公共课儿童     {         [键]         public int ChildID {get;组; }

    [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; }
}

1 个答案:

答案 0 :(得分:0)

您可以使用此方法:创建History模型。这包含一个像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; }
}

和Enum:

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

例如,当您编辑1 Child实体时,请更改属性名称和值,将其ID,类型和其他内容(ChangeDate,ChangedUserId)更改为History并保存历史记录。如果3个属性将更改,则应保存3个历史实体。然后,您可以通过ModelId,ChangedUserId等加载(过滤)历史记录。