环境:.NET Framework 4.0, Code First ENtity Framework 4.x
Lambda表达式First和Where在取一条数据时可以互换。但是当关联多表并使用First时就会出现错误,下面是错误之一:
Unable to create a constant value of type 'ICStars2_0.Model.Article2Category'. Only primitive types or enumeration types are supported in this context.
报错代码:
using (var db = new DbContext())
{
return
db.Categories.First(c => db.Article2Categories.Any(
a2c =>
a2c.CategoryID == c.ID &&
db.Articles.Any(a => a.UrlTitle.Equals(urlTitle) && a.ID == a2c.ArticleID)));
}
解决方案:
using (var db = new DbContext())
{
return
db.Categories.Where(
c =>
db.Article2Categories.Any(
a2c =>
a2c.CategoryID == c.ID &&
db.Articles.Any(a => a.UrlTitle.Equals(urlTitle) && a.ID == a2c.ArticleID))).First();
}
对应SQL语句:
select * from category where exists(
select * from article2category where category.ID=CategoryID and exists(
select * from article where ID=article2category.ArticleID and UrlTitle='urlTitle'
)
)
当使用Resharper重构时,就会把正确的WHERE子句改成FIRST子句,进而报错。 有时需要慎用Resharper, 它会重构出冗余代码又可能会重构出错误。
分享到:
相关推荐
Entity Framework5.0.0 CodeFirst入门教程
CodeFirst使用Entity._Framework编程
entityframework框架源代码,需要深入了解entityframework的同学可以下载深入学习研究
用于演示EntityFramework6在CodeFirst模式下如何连接Oracle数据库的实例Demo 使用CRUD方式创建数据表,详细连接说明见代码注释,只需要更换连接字符串中的账户和数据库地址即可 .Net框架为4.5请注意匹配
of Entity Framework, which is covered in the first edition of Programming Entity Framework. In 2010, Microsoft .NET 4 was released; it contained the next version of Entity Framework, referred to as ...
Model that is core to Entity Framework Rather than using a visual designer Code First allows you to create the model from your existing classes This book is dedicated to teaching readers how to use ...
Code First 使用Entity Framework编程,来源于园子里的翻译文章,讲得非常好!自己看完整PDF收藏,现共享给大家!
This is the best ebook for Entity framework code first!
我用来学习Code First的资料 挺好的,有3个文档,打包了
EF实体框架有3种类型,Data First、Model First、Code First。Code First是最简单用的最多的一种
Entity_Framework_Code_First_Succinctly.pdf 国外Entity_Framework_Code_First学习入门与进阶教程,PDF版的 方便学习
EntityFramework数据持久化复习资料4、Lambda表达式的使用(重点内容)(包含源码示例)
Entity Framework Code First is the latest edition of Microsoft’s flagship data access technology. It sits on the “classic” Entity Framework, which has existed since 2009. Entity Framework already ...
Entity Framework Code-First enables developers to read and write data in a relational database system using C# or VB.NET. It is Microsoft's answer to demand for an ORM from .NET developers. This book ...
如何用Code First 方式使用Entity Framework。