EntityFramework:值不能为 null。参数名: entitySet 异常解决方案
昨天EF莫名其妙的,掉所有接口访问都出现如下错误:百度,Google了半天,倒是有很多人都遇到了这个问题,但都没有一个解决方案,或者解决方案无效。通过层层排除,终于找到问题的所在。记录下来,给以后再遇到此问题的朋友们节省一些找问题的时间。而且这错误的原因如果不细心还真不好找。
异常信息如下:
{"Message":"出现错误。","ExceptionMessage":"值不能为 null。\r\n参数名: entitySet","ExceptionType":"System.ArgumentNullException","StackTrace":" 在 System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)\r\n 在 System.Data.Entity.Core.Mapping.EntitySetMapping..ctor(EntitySet entitySet, EntityContainerMapping containerMapping)\r\n 在 System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.AddEntitySetMapping(DbDatabaseMapping databaseMapping, EntitySet entitySet)\r\n 在 System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping)\r\n 在 System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping)\r\n 在 System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel)\r\n 在 System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.GenerateDatabaseMapping(EdmModel model, DbProviderInfo providerInfo, DbProviderManifest providerManifest)\r\n 在 System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)\r\n 在 System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)\r\n 在 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)\r\n 在 System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)\r\n 在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext()\r\n 在 System.Data.Entity.Internal.InternalContext.Initialize()\r\n 在 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n 在 System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()\r\n 在 System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()\r\n 在 System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()\r\n 在 System.Data.Entity.QueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken)\r\n 在 System.Data.Entity.QueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate)\r\n 在 BellChat.Business.UserService.<Login>d__4.MoveNext()
值不能为 null。参数名: entitySet。 表明是因为DBmodel 和数据库表格映射过程出现的问题,可以从model和表字段对应不上,或者数据库字段类型EF无法识别,这两点着手寻找错误的根源。
我这边最后找到的原因如下:
我有一处Include代码:
var result = db.Conversations.Where(m => (m.UserId == user.Id || m.TargetUserId == user.Id) && m.Id > req.Begin)
.Include(m => m.TargetUser).Include(m => m.LastMsg).Take(req.Count);
其中的TargetUser对象并不是对应数据库中的user表DBModel,而是一个自定义的responseModel。导致EF的Include失败,而这种错误完全不影响编译,调试时也无法定位到Include(m => m.TargetUser)这句代码,且该错误不仅仅只影响这一处接口调用的结果,它会导致整个EF数据库操作都报错,让你摸不着头脑。就感觉是EF在系统启动的时候先阅读整个程序代码,提前映射所有model,只要有一个model映射不上,整个EF就不能用。
解决办法如下:
将.Include(m => m.TargetUser)中的TargetUser改成数据库model就好了
public partial class Conversation
{
[JsonIgnore]
public User TargetUser { get; set; } //原来是public SeekerSimple TargetUser { get; set; }
public Message LastMsg { get; set; }
}
EntityFramework:值不能为 null。参数名: entitySet 异常解决方案的更多相关文章
- 值不能为null.参数名: viewInfo,如何解决
有蓝队网络服务器租用客户反映在一台服务器上使用数据库管理工具时弹出了如下错误 :值不能为null.参数名: viewInfo (Microsoft.SqlServer.Management.SqlSt ...
- MSSQL2008 R2 数据库展开报错:值不能为空 参数名:viewInfo
打开数据库时报错,提示应用程序组件中发生了无法处理的异常.如果单击“继续”,应用程序将忽略此错误并尝试继续. 针对此类问题的解决办法是:将路径C:\Documentsand Settings\Admi ...
- 值不能为 null。 参数名: source
今天调试程序总是报一个异常: 值不能为 null.参数名: source 异常详细信息: System.ArgumentNullException: 值不能为 null.参数名: source 通过断 ...
- Web Performance Test : 为Request的Post参数名添加XPath支持
问题描述 本文的标题看起来有些含糊其辞,这里我需要把问题阐述得更加清楚.这是我们使用VSTS进行Web Performance Test时,Asp.net造成的特定问题(也许其他开发工具或插件也会造成 ...
- 使用水晶报表更新后出现“值不能为 null。 参数名: inputString”
简单记录一下: 如果更新完水晶报表相关页面可能在原来页面刷新会出现错误:"值不能为 null. 参数名: inputString",如图:
- 值不能为 null 或为空。参数名: linkText
“/”应用程序中的服务器错误. 值不能为 null 或为空.参数名: linkText 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的 ...
- iBatis System.ArgumentNullException : 值不能为 null。 参数名: path2
System.ArgumentNullException : 值不能为 null. 参数名: path2 在app.config 或 web.config 中加上配置就可以了 <appSetti ...
- c# 异常:值不能为 null。 参数名: source
异常详细信息: System.ArgumentNullException: 值不能为 null.参数名: source 其实问题那就出在 Select() 方法,在 Select 上按 F12 查看定 ...
- Value cannot be null or empty. 参数名: contentPath
代码:<img src="@Url.Content(item.ThumbPath)" width="160" height="250" ...
随机推荐
- Windows下MongoDB安装与设置
最近在研究传奇已久的MongoDB,遇到很多问题,将整理好的Windows下的MongoDB整体到连接过程分享给大家,也留给自己. 第一步:下载http://www.mongodb.org/downl ...
- 微信公众平台开发教程--方培工作室,PHP语言版本
准备工作 微信公众平台的注册 介绍如何注册一个微信公众账号. 入门教程 微信公众平台开发入门教程 内容:1.申请SAE作为服务器; 2.启用开发模式; 3.微信公众平台PHP SDK; 4.接收发送消 ...
- ASN.1(抽象语法标记)
一.简介 ASN.1是一种对分布式计算机系统间交换的数据消息进行抽象描述的规范化语言. 二.教程 http://www.epubit.com.cn/book/onlinechapter/14877
- 【转】LINUX 5 常用ftp telnet配置
LINUX 5 常用ftp telnet配置 一.解决远程登陆乱码问题 目标:在xwindow和其console中使用中文界面,在纯console中使用英文 在/etc/profile最后加上一行 e ...
- [瞎JB写] C++多态
似乎只能通过引用或者指针进行动态多态...蛋疼的语法 #include <iostream> #include <vector> #include <memory> ...
- linux网络相关配置文件
linux系统一般来说分为两大类:1.RedHat系列:Redhat.Centos.Fedora等:2.Debian系列:Debian.Ubuntu等. linux系统中,TCP/IP网络是通过若干个 ...
- Oracle索引(B*tree和Bitmap)学习
在Oracle中,索引基本分为以下几种:B*Tree索引,反向索引,降序索引,位图索引,函数索引,interMedia全文索引等,其中最常用的是B*Tree索引和Bitmap索引. (1).与索引相关 ...
- zookeeper中Watcher和Notifications
问题导读:1.zookeeper观察者什么时候调用?2.传统远程轮询服务存在什么问题?3.zk中回调服务的机制是什么?4.zk中watcher为什么不永久注册?5.什么是znode? 在阅读之前首先明 ...
- NOIP2007 T1奖学金 解题报告-S.B.S.
洛谷P1093 题目描述 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金.期末,每个学生都有3门课的成绩:语文.数学.英语.先按总分从高到低排序,如果两个同学总分相同, ...
- JS实现图片上传预览效果:方法一
<script type="text/javascript"> //处理file input加载的图片文件 $(document).ready(function(e) ...