将更改应用于模型

Apply Changes To Model

我正在使用 MVC 和 entity Framework 通过存储库保存数据。

通常是视图模型,和实体数据库模型略有不同,所以我在存储库中有这样的东西。有没有更好的方法?

private void ApplyChangesToModel(AttributeImportModel entity, AttributeImport dbDef)
    {
        dbDef.ApplicationID = entity.ApplicationID;
        dbDef.Canceled = entity.Canceled;
        dbDef.DateRequested = entity.DateRequested;
        dbDef.Processed = entity.Processed;
        dbDef.ScheduleID = entity.ScheduleID;
        //dbDef.UniqueID = entity.UniqueID;
        dbDef.UserID = entity.UserID;
        dbDef.UserIP = entity.UserIP;
    }

    private void ApplyChangesToModel(AttributeImport dbDef, AttributeImportModel entity)
    {
        entity.AttributeImportID = dbDef.AttributeImportID;
        entity.ApplicationID = dbDef.ApplicationID;
        entity.Canceled = dbDef.Canceled;
        entity.DateRequested = dbDef.DateRequested;
        entity.Processed = dbDef.Processed;
        entity.ScheduleID = dbDef.ScheduleID;
        //dbDef.UniqueID = entity.UniqueID;
        entity.UserID = dbDef.UserID;
        entity.UserIP = dbDef.UserIP;
    }

    private void ApplyChangesToModel(AttributeImportModel dbDef, AttributeImportModel entity)
    {
        entity.AttributeImportID = dbDef.AttributeImportID;
        entity.ApplicationID = dbDef.ApplicationID;
        entity.Canceled = dbDef.Canceled;
        entity.DateRequested = dbDef.DateRequested;
        entity.Processed = dbDef.Processed;
        entity.ScheduleID = dbDef.ScheduleID;
        //dbDef.UniqueID = entity.UniqueID;
        entity.UserID = dbDef.UserID;
        entity.UserIP = dbDef.UserIP;
    }

考虑使用 AutoMapper:

Mapper.CreateMap<AttributeImportModel, AttributeImport>();
Mapper.Map(dbdef, entity); //Mapper.Map(source, target)