实体框架组通过导致无法施放类型的对象。键入System.String'

发布于 2025-02-11 02:27:59 字数 616 浏览 0 评论 0原文

我正在尝试遵循EF查询并在数据库的某些情况下返回例外,

//Contacts Stats  summary 
Dictionary<ContactStatusEnum, int> contactStats = (await context.Contacts.ToListAsync())
                                            .GroupBy(n => n.Status)
                                            .ToDictionary(x => x.Key, x => x.Count());
                        
                

这导致了异常 异常:无法将“ system.dbnull”类型的对象施放到type“ system.string”。

同时,我执行了SQL

SELECT status,COUNT(*) FROM contacts group BY STATUS

,并在没有任何错误的情况下被执行。另外,所有联系记录都存在状态价值,

同一代码在舞台或开发数据库中工作

I am trying to run following EF query and return the exception on some instances of Databases

//Contacts Stats  summary 
Dictionary<ContactStatusEnum, int> contactStats = (await context.Contacts.ToListAsync())
                                            .GroupBy(n => n.Status)
                                            .ToDictionary(x => x.Key, x => x.Count());
                        
                

This is causing an exception
Exception:Unable to cast object of type 'System.DBNull' to type 'System.String'.

At same time i executed sql

SELECT status,COUNT(*) FROM contacts group BY STATUS

and is getting executed without any errors. Plus there are status value exists for all contact records

The same code is working agaist the stage or DEV db

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2025-02-18 02:27:59

您可以按照下面的方式进行修改

//Contacts Stats  summary 
Dictionary<ContactStatusEnum, int> contactStats = (await context.Contacts.Where(cn=>cn.status!=null).ToListAsync())
                                        .GroupBy(n => n.Status)
                                        .ToDictionary(x => x.Key, x => x.Count());

,请按照此操作

//Contacts Stats  summary 
Dictionary<ContactStatusEnum, int> contactStats = (await context.Contacts.ToListAsync())
                                    .GroupBy(n => n.Status ?? string.Empty)
                                    .ToDictionary(x => x.Key, x => x.Count());

you can modify as below

//Contacts Stats  summary 
Dictionary<ContactStatusEnum, int> contactStats = (await context.Contacts.Where(cn=>cn.status!=null).ToListAsync())
                                        .GroupBy(n => n.Status)
                                        .ToDictionary(x => x.Key, x => x.Count());

else follow this one

//Contacts Stats  summary 
Dictionary<ContactStatusEnum, int> contactStats = (await context.Contacts.ToListAsync())
                                    .GroupBy(n => n.Status ?? string.Empty)
                                    .ToDictionary(x => x.Key, x => x.Count());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文