C# 使用AutoMapper将项目的ListBoxFor映射到ICollection

C# 使用AutoMapper将项目的ListBoxFor映射到ICollection,c#,entity-framework,automapper,C#,Entity Framework,Automapper,如何配置AutoMapper将整数数组(从multi-select MVC ListBoxFor元素填充)映射到域对象的ICollection属性?基本上,我想将域对象的PatientTypes和ProviderTypes属性设置为用户在列表框中选择的任何属性,然后将对象保存回数据库 域对象 public class Document { public int ID { get; set; } public virtual ICollection<PatientType&

如何配置AutoMapper将整数数组(从multi-select MVC ListBoxFor元素填充)映射到域对象的ICollection属性?基本上,我想将域对象的PatientTypes和ProviderTypes属性设置为用户在列表框中选择的任何属性,然后将对象保存回数据库

域对象

public class Document
{
    public int ID { get; set; }

    public virtual ICollection<PatientType> PatientTypes { get; set; }
    public virtual ICollection<ProviderType> ProviderTypes { get; set; }
}
控制器

public virtual ActionResult Edit(int pid)
{
    var model = Mapper.Map<DocumentEditModel>(_documentRepository.Find(pid));
    model.ProviderTypeList = new SelectList(_providerTypeRepository.All.OrderBy(x => x.Value), "ID", "Value");
    model.PatientTypeList = new SelectList(_patientTypeRepository.All.OrderBy(x => x.Value), "ID", "Value");

    return View(model);
}

[HttpPost]
public virtual ActionResult Edit(DocumentEditModel model)
{
    if (ModelState.IsValid)
    {
        var document = Mapper.Map(model, _documentRepository.Find(model.ID));
        document.DateModified = DateTime.Now;

        _documentRepository.InsertOrUpdate(document);
        _documentRepository.Save();

        return null;
    }

    model.ProviderTypeList = new SelectList(_providerTypeRepository.All.OrderBy(x => x.Value), "ID", "Value");
    model.PatientTypeList = new SelectList(_patientTypeRepository.All.OrderBy(x => x.Value), "ID", "Value");

    return View(model);
}
公共虚拟操作结果编辑(int-pid)
{
var model=Mapper.Map(_documentRepository.Find(pid));
model.ProviderTypeList=新的SelectList(_providerTypeRepository.All.OrderBy(x=>x.Value),“ID”,“Value”);
model.PatientTypeList=新建SelectList(_patientTypeRepository.All.OrderBy(x=>x.Value),“ID”,“Value”);
返回视图(模型);
}
[HttpPost]
公共虚拟操作结果编辑(DocumentEditModel模型)
{
if(ModelState.IsValid)
{
var document=Mapper.Map(model,_documentRepository.Find(model.ID));
document.DateModified=DateTime.Now;
_documentRepository.InsertOrUpdate(文档);
_documentRepository.Save();
返回null;
}
model.ProviderTypeList=新的SelectList(_providerTypeRepository.All.OrderBy(x=>x.Value),“ID”,“Value”);
model.PatientTypeList=新建SelectList(_patientTypeRepository.All.OrderBy(x=>x.Value),“ID”,“Value”);
返回视图(模型);
}
自动映射配置

Mapper.CreateMap<Document, DocumentEditModel>();
Mapper.CreateMap<DocumentEditModel, Document>();
Mapper.CreateMap();
CreateMap();

由于关联是多对多的,您只需在数据库中创建连接记录。一种方便的方法是清除集合并向其中添加项。让我们以
Document.PatientTypes
为例:

var document = Mapper.Map(model, _documentRepository.Find(model.ID));
document.DateModified = DateTime.Now;

// Set the new associatins with PatientTypes
document.PatientTypes.Clear();
foreach(var pt in model.PatientTypeList.Select(id => new PatientType{Id = id}))
{
    document.PatientTypes.Add(pt);
}

_documentRepository.InsertOrUpdate(document);
_documentRepository.Save();
(我不得不对财产名称做一些假设)

这里发生的情况是,
DocumentPatientTypes
连接表中的现有记录被一组新记录替换。这是通过使用所谓的
newpatientType
s来完成的。您不必首先从数据库中获取真实的连接记录,因为EF只需要Id值来创建新的连接记录


如你所见,我默默地将Automapper从等式中剔除。将整数列表映射到
PatientType
,会有点过分。
Select
非常简单,只要有一点经验,就可以立即识别存根实体模式,否则它将被
Mapper.Map
语句隐藏。

由于关联是多对多的,您只需在数据库中创建连接记录。一种方便的方法是清除集合并向其中添加项。让我们以
Document.PatientTypes
为例:

var document = Mapper.Map(model, _documentRepository.Find(model.ID));
document.DateModified = DateTime.Now;

// Set the new associatins with PatientTypes
document.PatientTypes.Clear();
foreach(var pt in model.PatientTypeList.Select(id => new PatientType{Id = id}))
{
    document.PatientTypes.Add(pt);
}

_documentRepository.InsertOrUpdate(document);
_documentRepository.Save();
(我不得不对财产名称做一些假设)

这里发生的情况是,
DocumentPatientTypes
连接表中的现有记录被一组新记录替换。这是通过使用所谓的
newpatientType
s来完成的。您不必首先从数据库中获取真实的连接记录,因为EF只需要Id值来创建新的连接记录


如你所见,我默默地将Automapper从等式中剔除。将整数列表映射到
PatientType
,会有点过分。
Select
非常简单,只要有一点经验,就可以立即识别存根实体模式,否则会被
Mapper.Map
语句隐藏。

文档-患者类型/提供者类型
多对多?是的,是的。我只是没有在这里说明这一点。
Document-PatientTypes/ProviderTypes
many-to-many?是的。我只是没有在这里说明这一点。这让我达到了90%的目标。我必须在调用.Add()之前将“pt”附加到DbContext,这在第一次使用时效果很好。但是,在第二次编辑时,我遇到了无法附加已附加的实体的错误。您的上下文(或
documentRepository
)的生命周期似乎太长。为每个请求创建一个新的。不幸的是,我工作的公司有一个复杂的上下文管理流程,他们要求我使用这个流程。话虽如此,我还是能够找到一种解决方法,使用您的解决方案作为基础。好的,做得好。尝试为健康的上下文生命周期管理提供有力的理由,但这对于EF的工作至关重要。而且更有趣!这让我有90%的路要走。我必须在调用.Add()之前将“pt”附加到DbContext,这在第一次使用时效果很好。但是,在第二次编辑时,我遇到了无法附加已附加的实体的错误。您的上下文(或
documentRepository
)的生命周期似乎太长。为每个请求创建一个新的。不幸的是,我工作的公司有一个复杂的上下文管理流程,他们要求我使用这个流程。话虽如此,我还是能够找到一种解决方法,使用您的解决方案作为基础。好的,做得好。尝试为健康的上下文生命周期管理提供有力的理由,但这对于EF的工作至关重要。而且更有趣!