【问题标题】:Mapping Collections with Automapper?使用 Automapper 映射集合?
【发布时间】:2015-03-18 02:50:29
【问题描述】:

我做了很多搜索,但不确定如何使用 Automapper 收集地图。

给定:

Public Class PostSummaryDTO
        Public Property PostId As Integer
        Public Property PostGuid As Guid
        Public Property PostTitle As String
        Public Property PostSummary As String
        Public Property PostDateCreated As DateTime
        Public Property PostIsPublished As Boolean
        Public Property PostText As String
        Public Property PostCategory As ICollection(Of be_Categories)
        Public Property PostTag As ICollection(Of be_PostTag)
        Public Author As String
    End Class

Public Class be_PostsViewModel
        Public Property Id As Integer
        Property Author As String
        <DisplayName("Title")> <Required(ErrorMessage:="Your post must have a title")>
            Public Property PostTitle As String
        <DisplayName("My Snarky Text")> Public Property PostSummary As String
        <DisplayName("Post")> Public Property PostText As String
        <UIHint("DateCreated")> <DisplayName("Date Created")> Property PostDateCreated    
            As DateTime?
        <DisplayName("Publish")> Public Property PostIsPublished As Boolean
        Public Property PostGuid As Guid
        Public Property BlogId As Guid
        <DataType(DataType.MultilineText)> <UIHint("Tags")> <DisplayName("Tags")>
           Public Property PostTags As ICollection(Of be_PostTag)
        <DisplayName("Category")> <UIHint("Categories")> Public Property
            PostCategory As ICollection(Of CategoriesViewModel)

    End Class

如何将Public Property PostCategory As ICollection(Of be_Categories) 映射到Public Property PostCategory As ICollection(Of CategoriesViewModel)

【问题讨论】:

标签: vb.net collections asp.net-mvc-5 automapper entity-framework-6.1


【解决方案1】:

创建be_CategoriesCategoriesViewModelPostSummaryDTObe_PostsViewModel 的映射后,AutoMapper 会自动理解并处理其余部分。

' Do this only once in your application
Mapper.CreateMap(Of be_Categories, CategoriesViewModel)()
Mapper.CreateMap(Of PostSummaryDTO, be_PostsViewModel)()

Dim yourDTO = New PostSummaryDTO()
' Set all your DTO values here

' Call AutoMapper
Dim vm As be_PostsViewModel = Mapper.Map(Of be_PostsViewModel)(yourDTO)

vm 现在应该很好地映射您的所有属性,包括您的Collection。它可以在映射集合的“内部类型”时自动映射集合。你可以看到所有支持的集合here

我刚刚对此进行了测试,它可以工作。

更新:如果您只想将集合直接从一个变量映射到另一个变量,您必须这样做:

' Create a mapping for the types inside the collection
Mapper.CreateMap(Of be_Categories, CategoriesViewModel)()

' Declare a collection of be_Categories
Dim col As New List(Of be_Categories)()
' Insert all the items
Dim cat = New be_Categories()
col.Add(cat)

' Call AutoMapper stating that you want a List as return
Dim vm As List(Of CategoriesViewModel) = Mapper.Map(Of List(Of CategoriesViewModel))(col)

同样,它会自动为您创建一个List(Of CategoriesViewModel),因为您为内部类型创建了映射。

有点不相关,但请注意 AutoMapper 不会理解 IdPostId 指的是同一个属性。 您可以了解 AutoMapper 的所有约定here

【讨论】:

  • “归因”到底是什么意思?抱歉这个愚蠢的问题,但我是 Automapper 的新手,你能举个例子吗?谢谢
  • 我的意思是你在你的源对象中设置了所有的值。喜欢dto.PostTitle = "Title"dto.PostText = "Text"。我在答案中使用的代码是一个完整的工作示例。你试过了吗?
猜你喜欢
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
相关资源
最近更新 更多