首页 > 解决方案 > 将实体映射到模型

问题描述

我有一个带有字符串 _description 和 List 注释的结构。我怎样才能将它们都映射,以便它可能发生这样的事情?

public IObjective MapEntityToModel(IObjectiveEntity objectiveEntity)
    {
        return new Objective
            (
                objectiveEntity._Id,
                objectiveEntity.Name,
                objectiveEntity.Description,
                objectiveEntity.Comments,
                (PriorityType)Enum.Parse(typeof(PriorityType), objectiveEntity.PriorityType)
            );
    }

如您所见,Description 和 Comments 下面是结构的两种类型,其中 Comments 是字符串列表。错误消息显示我没有接受 5 个参数的构造函数,这当然是因为我在 Obective 类的构造函数中定义了一个结构而不是字符串和字符串列表。我应该如何进行?

标签: c#databaseoopmappingstructure

解决方案


  public IObjective MapEntityToModel(IObjectiveEntity objectiveEntity)
    {
        return new Objective
            (
                objectiveEntity._Id,
                objectiveEntity.Name,
                new TaskDetails
                (
                    objectiveEntity.Description,
                    objectiveEntity.Comments
                ),
                (PriorityType)Enum.Parse(typeof(PriorityType), objectiveEntity.PriorityType)
            );
    }

谢谢你们!我需要调用新的 TaskDetails,这就是它的修复方法!:) 这就是从数据库中移出时的工作原理!


推荐阅读