Automapper - 嵌套类型的AutoMapperMappingException和NullReferenceException(Automapper - AutoMapperMappingException and NullReferenceException for nested type)
我有一个Tag类和一个相应的TagDto类,我想从Tag到TagDto。
由于我使用EF Core,我有一个名为MoneyItemTag的类,它代表了多对多的关系。
这是我的课程:
public abstract class MoneyItemBase { public int Id { get; set; } public string ItemText { get; set; } public decimal Amount { get; set; } public MoneyItemType MoneyItemType { get; protected set; } public ICollection<MoneyItemTag> MoneyItemTags { get; set; } } public class MoneyItemTag { public int MoneyItemId { get; set; } public MoneyItemBase MoneyItem { get; set; } public int TagId { get; set; } public Tag Tag { get; set; } } public class Tag { public int TagId { get; set; } public string TagName { get; set; } public ICollection<MoneyItemTag> MoneyItemTags { get; set; } } [Fact] public void Tag_TagDto_Mapping() { Mapper.Initialize(cfg => { //cfg.AddProfile<MappingProfile>(); cfg.CreateMap<Tag, TagDto>() .ForMember(x => x.MoneyItems, opts => opts.MapFrom(src => src.MoneyItemTags.Select(x => x.MoneyItem))); cfg.CreateMap<MoneyItemBase, MoneyItemBaseDto>() .ForMember(x => x.Tags, opts => opts.MapFrom(src => src.MoneyItemTags.Select(y => y.Tag.TagName).ToList())); }); MoneyItemTag mo = new MoneyItemTag(); mo.MoneyItem = new SingleIncome() { Id = 2, ItemText = "test", Active = false, DueDate = DateTime.Now, Amount = 33 }; mo.MoneyItemId = 2; var mit = new List<MoneyItemTag>() { mo }; Tag tag = new Tag() { TagId = 2, TagName = "test", MoneyItemTags = mit }; mo.TagId = 2; mo.Tag = tag; var dto = Mapper.Map<TagDto>(tag); Assert.NotNull(dto);
而这种关系造成了一些麻烦。 当我运行以下测试或当我想在我的Asp.Net核心应用程序中进行映射时,出现以下异常:
AutoMapper.AutoMapperMappingException : Error mapping types. Mapping types: Tag -> TagDto HouseholdBook.Data.Model.Tag -> HouseholdBook.Dto.TagDto Type Map configuration: Tag -> TagDto HouseholdBook.Data.Model.Tag -> HouseholdBook.Dto.TagDto Property: MoneyItems ---- AutoMapper.AutoMapperMappingException : Error mapping types. Mapping types: MoneyItemBase -> MoneyItemBaseDto HouseholdBook.Data.Model.MoneyItemBase -> HouseholdBook.Dto.MoneyItemBaseDto Type Map configuration: MoneyItemBase -> MoneyItemBaseDto HouseholdBook.Data.Model.MoneyItemBase -> HouseholdBook.Dto.MoneyItemBaseDto Property: Id -------- System.NullReferenceException : Object reference not set to an instance of an object.
这里有什么问题? 我从异常消息中看不到我缺少的东西。
I have a Tag class and a corresponding TagDto class which I want to map from Tag to TagDto.
Due to my usage of EF Core I have a Collection to a class called MoneyItemTag which represents a many-to-many relationship.
Here are my classes:
public abstract class MoneyItemBase { public int Id { get; set; } public string ItemText { get; set; } public decimal Amount { get; set; } public MoneyItemType MoneyItemType { get; protected set; } public ICollection<MoneyItemTag> MoneyItemTags { get; set; } } public class MoneyItemTag { public int MoneyItemId { get; set; } public MoneyItemBase MoneyItem { get; set; } public int TagId { get; set; } public Tag Tag { get; set; } } public class Tag { public int TagId { get; set; } public string TagName { get; set; } public ICollection<MoneyItemTag> MoneyItemTags { get; set; } } [Fact] public void Tag_TagDto_Mapping() { Mapper.Initialize(cfg => { //cfg.AddProfile<MappingProfile>(); cfg.CreateMap<Tag, TagDto>() .ForMember(x => x.MoneyItems, opts => opts.MapFrom(src => src.MoneyItemTags.Select(x => x.MoneyItem))); cfg.CreateMap<MoneyItemBase, MoneyItemBaseDto>() .ForMember(x => x.Tags, opts => opts.MapFrom(src => src.MoneyItemTags.Select(y => y.Tag.TagName).ToList())); }); MoneyItemTag mo = new MoneyItemTag(); mo.MoneyItem = new SingleIncome() { Id = 2, ItemText = "test", Active = false, DueDate = DateTime.Now, Amount = 33 }; mo.MoneyItemId = 2; var mit = new List<MoneyItemTag>() { mo }; Tag tag = new Tag() { TagId = 2, TagName = "test", MoneyItemTags = mit }; mo.TagId = 2; mo.Tag = tag; var dto = Mapper.Map<TagDto>(tag); Assert.NotNull(dto);
And this relationship is causing some trouble. When I run the following test or when I want make the mapping in my Asp.Net core application, I get the following exception:
AutoMapper.AutoMapperMappingException : Error mapping types. Mapping types: Tag -> TagDto HouseholdBook.Data.Model.Tag -> HouseholdBook.Dto.TagDto Type Map configuration: Tag -> TagDto HouseholdBook.Data.Model.Tag -> HouseholdBook.Dto.TagDto Property: MoneyItems ---- AutoMapper.AutoMapperMappingException : Error mapping types. Mapping types: MoneyItemBase -> MoneyItemBaseDto HouseholdBook.Data.Model.MoneyItemBase -> HouseholdBook.Dto.MoneyItemBaseDto Type Map configuration: MoneyItemBase -> MoneyItemBaseDto HouseholdBook.Data.Model.MoneyItemBase -> HouseholdBook.Dto.MoneyItemBaseDto Property: Id -------- System.NullReferenceException : Object reference not set to an instance of an object.
What is wrong here? I cannot see from the exception message what I am missing.
原文:https://stackoverflow.com/questions/43968442
最满意答案
当对方收到FIN时:
read()
将返回-1readLine()
将返回nullreadXXX()
将为任何其他X引发EOFException
。什么都不会发生:
Socket.isClosed()
不会奇迹般地变为真Socket.isConnected()
不会奇迹般地变成错误。When the peer receives a FIN:
read()
will return -1readLine()
will return nullreadXXX()
will throwEOFException
for any other X.What won't happen:
Socket.isClosed()
won't magically become trueSocket.isConnected()
won't magically become false.
相关问答
更多-
当套接字处于FIN_WAIT_2中时,本地套接字已关闭,正在等待远程套接字发送其关闭请求。 如果这个关闭请求永远不会到达,套接字将保持FIN_WAIT_2状态一段时间。 其原因是,如果来自远程方的关闭请求将被延迟并在另一个应用程序重新使用该套接字后到达,则该新连接将立即关闭。 如果需要,可以更改超时值,但最终,非托管dll尚未完全实现TCP关闭序列。 有关更多信息,请参阅 http://answers.microsoft.com/en-us/windows/forum/windows_7-networkin ...
-
以下是我们迄今为止所学到的内容,希望它可以帮助其他人: 在Oracle JRE 1.7.0_51-b13中,当用户java.net.HttpURLConnection和javax.net.ssl.HttpsURLConnection(java.net.URLConnection的两个子类)启用HTTP KeepAlive时,服务器终止HTTP连接(因为它允许按照RFC),JRE不适当地继续使用断开连接的套接字(当该套接字处于看似FIN_WAIT状态之一时)。 这样做时,JRE在套接字发送上“等待”(永远不会 ...
-
您可以通过侦听同一对象上的特殊error事件来捕获这些错误: client.on('error', function(err) { console.log('Uh oh, an error occurred: ' + err); }); // ... client.write(data); You can catch these errors by listening for the special error event on the same object: client.on('error' ...
-
根据RFC 793中的一些段落 “3.如果连接处于同步状态(ESTABLISHED,FIN-WAIT-1,FIN-WAIT-2,CLOSE-WAIT,CLOSING,LAST-ACK,TIME-WAIT),则任何不可接受的段(窗口序列号外)或不可接受的确认号)必须仅引出包含当前发送序列号的空确认段和表示预期接收的下一个序列号的确认,并且连接保持相同状态。 .... “考虑处理传入段的一种自然方式是想象它们首先被测试正确的序列号(即,它们的内容位于序列号空间中预期的”接收窗口“的范围内)然后它们是通常按顺序编 ...
-
读完man epoll后我得到了理由。 它是由于不合适的代码而发生的。 epoll_ctl(epollfd_, EPOLL_CTL_DEL, sockfd, &ev) 似乎在github上关于epoll的正确使用样本很少。 I got the reason after read man epoll. it happens due to unsuitable code. epoll_ctl(epollfd_, EPOLL_CTL_DEL, sockfd, &ev) it seems there is ...
-
当对方收到FIN时: read()将返回-1 readLine()将返回null readXXX()将为任何其他X引发EOFException 。 什么都不会发生: Socket.isClosed()不会奇迹般地变为真 Socket.isConnected()不会奇迹般地变成错误。 When the peer receives a FIN: read() will return -1 readLine() will return null readXXX() will throw EOFException ...
-
发送错误和重新连接正在被激活。 我的错。 我想,盯着日志太久了。 谢谢! The send error and re-connect is being propgated. My bad. Staring at logs too long I guess. THANKS!
-
您想要使用shutdown(s, SHUT_WR)套接字的写入端。 shutdown()调用会导致与sockfd关联的套接字上的全双工连接全部或部分关闭。 如果SHUT_RD如何,将不允许进一步接收。 如果SHUT_WR如何,则SHUT_WR进一步传输。 如果SHUT_RDWR如何,将不允许进一步的接收和传输。 也可以看看: 关闭vs关机插座? You want to shutdown your write-side of the socket with shutdown(s, SHUT_WR). The ...
-
Websocket客户端在握手后立即发送[FIN,ACK](Websocket client send [FIN, ACK] immediately after handshake)[2023-03-16]
Firefox版本使用websocket RFC6455协议标准(协议版本13),QT 4.8.1使用websocket hybi-00协议标准(无协议版本) https://tools.ietf.org/html/draft-ietf-hybi- thewebsocketprotocol-00 也许你的服务器正确实现了RFC6455协议标准,但错误地使用了hybi-00协议标准。 请通过记录您的请求和响应内容(/ key3 /和质询数据)进行调试。 如果服务器生成的质询数据与客户端(QT)中的质询数据不匹 ... -
如果您正在谈论使用Php连接到您的Websocket,那么YES ,它是可行的。 有一些像Php WebSocket客户端或Websocket php这样的库可以作为客户端连接到您的websocket。 您也可以查看此Websocket客户端以获取示例。 另一个工作示例没有使用任何库并按照规范实现hybi10帧编码 : $host = 'www.host.com'; // your websocket server $port = 443; $local = "http://localhost"; // ...