将更改应用于模型

时间:2015-02-09 06:59:56

标签: asp.net-mvc entity-framework-6

我正在使用MVC和实体框架来通过存储库保存数据。

通常视图模型和entityDB模型略有不同,所以我在存储库中有这样的东西。还有更好的方法吗?

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

1 个答案:

答案 0 :(得分:1)

考虑使用AutoMapper:

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