【问题标题】:DTO mapping exception with entity in ABPDTO 映射异常与 ABP 中的实体
【发布时间】:2018-01-01 16:42:30
【问题描述】:

我在尝试插入实体时收到关于“映射”的错误。

插入是通过 CrudAppService 的Create 方法进行的。 我的实体继承自 FullAuditedEntity,但相关的 DTO 仅指定了几个属性。

我该如何处理这种情况?

Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
==========================================================================
PostDto -> Post (Destination member list)
MaiPiuSprechi.Domain.Posts.Dto.PostDto -> MaiPiuSprechi.Domain.Posts.Post (Destination member list)

Unmapped properties:
Items
IsDeleted
DeleterUser
DeleterUserId
DeletionTime
CreatorUser
LastModifierUser
LastModificationTime
LastModifierUserId
CreationTime
CreatorUserId

我的 DTO:

[AutoMapFrom(typeof(Post))]
public class PostDto : EntityDto
{
    [Required]
    public string Description { get; set; }
    public string Note { get; set; }
    public DateTime? Scadenza { get; set; }

    [Required]
    public string Zona { get; set; }
    public TipoPost Tipo { get; set; }
}

我的实体:

[Table("AbpPosts")]
public class Post : FullAuditedEntity<int,User>
{
    public Post()
    {
        // CreationTime = DateTime.Now;
    }

    public Post(string description, string zona)
    {
        Description = description;
        Zona = zona;
    }

    public Post(string description, string zona, TipoPost tipo)
    {
        Description = description;
        Zona = zona;
        Tipo = tipo;
    }

    [Required]
    public string Description { get; set; }
    public string Note { get; set; }
    public DateTime? Scadenza { get; set; }

    [Required]
    public string Zona { get; set; }

    [NotMapped]
    public virtual ICollection<Item> Items { get; set; }
    public TipoPost Tipo { get; set; }
}

【问题讨论】:

    标签: c# automapper aspnetboilerplate


    【解决方案1】:

    所需的映射方向:

    PostDto -> Post(目标成员列表)

    [AutoMapFrom(typeof(Post))] 在这里配置Post -&gt; PostDto

    [AutoMapFrom(typeof(Post))]
    public class PostDto : EntityDto
    

    要配置两个方向,只需执行以下操作:

    [AutoMap(typeof(Post))]
    public class PostDto : EntityDto
    

    【讨论】:

    • Brilliant 为我节省了数小时的时间。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    相关资源
    最近更新 更多