gpt4 book ai didi

c# - EmitMapper 的集合列表映射问题

转载 作者:行者123 更新时间:2023-11-30 18:05:10 24 4
gpt4 key购买 nike

源类:

public class Post
{
public long ID { get; set; }

[Column(TypeName="nvarchar")]
[Required]
[StringLength(250)]
public string Name { get; set; }

[Column(TypeName="varchar")]
[StringLength(250)]
public string UrlName { get; set; }

[Column(TypeName="ntext")]
public string Excerpt { get; set; }

[Column(TypeName="ntext")]
[Required]
public string Content { get; set; }

public DateTime PostedTime { get; set; }
public DateTime? PublishedTime { get; set; }
public DateTime? LastUpdatedTime { get; set; }
public bool IsPublished { get; set; }

public virtual List<Category> Categories { get; set; }
public virtual List<Comment> Comments { get; set; }
public virtual List<Tag> Tags { get; set; }
}

目标类

public class Post : Model
{
public long ID { get; set; }
public string Name { get; set; }
public string UrlName { get; set; }
public string Excerpt { get; set; }
public string Content { get; set; }
public DateTime PostedTime { get; set; }
public DateTime LastCommentedTime { get; set; }
public bool IsPublished { get; set; }

public List<Category> Category { get; set; }
public List<Comment> Comments { get; set; }
public List<Tag> Tags { get; set; }
}

我尝试使用 EmitMapper 相互映射;从source映射到desction时,代码示例如下:

[TestMethod]
public void ShouleMapEntityToModel()
{
Post eP = new Post();
eP.ID = 2;
eP.Comments = new List<Comment>();

eP.Comments.Add(new Comment()
{
ID = 2,
Author = "derek"
});

var mP = eP.Map<Post, mBlog.Core.Models.Post>();

Assert.IsNotNull(mP);
Assert.AreEqual(1, mP.Comments.Count());
}

我遇到了一个异常(exception),

测试方法 mBlog.Test.EmitMapperTest.ShouleMapEntityToModel 抛出异常:System.Exception:在 System.Collections.Generic.IList`1[[mBlog.Core.Models.Post, mBlog.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 中找不到类型 [] 的构造函数

最佳答案

我遇到了同样的问题,但我找到了解决方案。不要为您的目标对象使用列表。如果您在 mBlog.Core.Models.Post 对象中使用简单的数组,您应该得到一个很好填充的对象。所以你的目标类应该是这样的:

public class Post : Model
{
public long ID { get; set; }
public string Name { get; set; }
public string UrlName { get; set; }
public string Excerpt { get; set; }
public string Content { get; set; }
public DateTime PostedTime { get; set; }
public DateTime LastCommentedTime { get; set; }
public bool IsPublished { get; set; }

public Category[] Category { get; set; }
public Comment[] Comments { get; set; }
public Tag[] Tags { get; set; }
}

关于c# - EmitMapper 的集合列表映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5834574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com