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" ...
随机推荐
- Eclipse启动报错:An internal error occurred during: "Initializing Java Tooling".
An internal error occurred during: "Initializing Java Tooling".java.lang.NullPointerExcept ...
- PL/SQL之--变量
一.PL/SQL 简介 PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL).PL/SQL是oracle对sql语句的一种扩展,在普通SQL语句的使用上 ...
- Oracle-创建服务器参数文件
允许使用传统的init.ora或SPFILE作为配置文件.但是建议所有数据库创建和使用一个SPFILE.可以从init.ora创建SPFILE SQL> CREATE spfile FROM p ...
- kafka常用的操作命令
1.kafka启动命令 nohup bin/kafka-server-start.sh config/server.properties & 2.创建topic bin/kafka-topic ...
- 如何使用emacs编写c语言程序,并编译运行
vi和emacs被分别被称为编辑器之神和神之编辑器.vi的入门精通都很难,emacs入门容易,精通难:vi使用起来不停地切换模式,而emacs则不停地ctrl,meta等组合键.因此,高德纳大师说操作 ...
- uniq
-c, --count 在每行前加上表示相应行目出现次数的前缀编号-d, --repeated 只输出重复的行-D, --all-repeated[=delimit-method 显示所有重复的行de ...
- Python 基本类型转换
python 有关字符串处理有哪些好用的方法?reverse len 字符串分割,合并?截取?查找? find index join split unicode字符串的表示 ""& ...
- Sql Practice 2
之前写了一个SP用来向dimention table插入0 -1 dummy row的值,但今天在process adventureworksdw2008示例 数据库的时候报错,查看了一下,是因为自己 ...
- CentOS安装Hypernetes相关问题解法
1.手动编译hyper缺少libdevmapper.h git clone -b v2_02_103 https://git.fedorahosted.org/git/lvm2.git /usr/lo ...
- 【温故而知新-Javascript】使用 Document 对象
Document 对象时通往DOM功能的入口,它向你提供了当前文档的信息,以及一组可供探索.导航.搜索或操作结构与内容的功能. 我们通过全局变量document访问Document对象,它是浏览器为我 ...