linq to sql: 在Entityfamework Core中使用多个DbContext
最近在学习DotNetCore并做一个自己的小项目,分为了多个数据库,AccountDbContext和BlogDbContext,
发blog的时候需要用到Account的信息,但是再Blog中只记录了UserID,然后用到了如下代码:
var q = from u in this.DbAccount.Users
join p in this.DbBlog.Posts
on u.UserID equals p.UserID
select u;
var v = q.FirstOrDefault();
但是目前来说貌似不支持,通过各种搜索,倒是有一个改xml的方案,但是我的项目的DbContext是手写而不是使用SqlMetal工具生成的。报错部分信息如下
"Microsoft.EntityFrame
workCore.Query","queryModel":"(from User u in DbSet<User> join Post p in DbSet<Post> on [u].UserID equals [p].UserID select [u]).F..."}}}}
'q.FirstOrDefault()' threw an exception of type 'System.ArgumentNullException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147467261
HelpLink: null
InnerException: null
Message: "Value cannot be null.\r\nParameter name: entityType"
ParamName: "entityType"
Source: "Microsoft.EntityFrameworkCore.Relational"
StackTrace: " at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)\r\n at Microsoft.EntityFrameworkCore.RelationalMetadataExtensions.Relational(IEntityType entityType)\r\n at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType)
但是在同一个server上夸数据库的查询再sqlserver和mysql都是支持的。也就是说EFCore 2.0现在不支持,现在临时的解决方案如下,
var userID = this.User.GetUserID();
var queryUsers = from u in this.DbAccount.Users.Include(a => a.Account)
where u.UserID == userID
select new
{
UserID = u.UserID,
UserNickName = u.NickName
};
var queryPosts = from p in this.DbBlog.Posts.Include(a => a.Tags)
join pc in this.DbBlog.PostContents
on p.PostID equals pc.PostID
orderby pc.CreateAt descending
where p.UserID == userID
select new
{
PostID = p.PostID,
UserID = p.UserID,
Title = p.Title,
URL = p.URL,
Summary = p.Summary,
Tags = p.Tags,
EditorType = pc.EditorType,
MD5Hash = pc.MD5Hash,
Content = pc.Content,
ContentCreateAt = pc.CreateAt
};
var queryPo = from u in queryUsers.ToList()
join p in queryPosts.ToList()
on u.UserID equals p.UserID
orderby p.ContentCreateAt
select new
{
UserNickName = u.UserNickName,
Title = p.Title
};
var pa = queryPo.ToList();
测试通过,但是要麻烦多了,就看EFCore什么时候能原生支持了。
linq to sql: 在Entityfamework Core中使用多个DbContext的更多相关文章
- 在Linq to sql 和 Entity framework 中使用lambda表达式实现left join
在Linq to sql 和 Entity framework 中使用lambda表达式实现left join 我们知道lambda表达式在Linq to sql 和 Entity framework ...
- EF Core中如何取消跟踪DbContext中所有被跟踪的实体
首先定义一个DbContext的扩展类DbContextDetachAllExtension,其中包含一个DbContext的扩展方法DetachAll,用来取消跟踪DbContext中所有被跟踪的实 ...
- LINQ之路10:LINQ to SQL 和 Entity Framework(下)
在本篇中,我们将接着上一篇“LINQ to SQL 和 Entity Framework(上)”的内容,继续使用LINQ to SQL和Entity Framework来实践“解释查询”,学习这些技术 ...
- LINQ之路 9:LINQ to SQL 和 Entity Framework(上)
在上一篇中,我们从理论和概念上详细的了解了LINQ的第二种架构“解释查询”.在这接下来的二个篇章中,我们将使用LINQ to SQL和Entity Framework来实践“解释查询”,学习这些技术的 ...
- LINQ To SQL
议程 1.LINQ To SQL概述 2.LINQ To SQL对象模型 3.LINQ To SQL查询 用到的数据库 SQL Server 2005,数据库名为Test. 两张表,分别为Studen ...
- Linq之Linq to Sql
目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍l ...
- 20150222—LINQ to SQL 插入、更新和删除
注意,使用LINQ to SQL时, 表中必须有一个主键才可以起效,否则系统将无法对数据作出修改 插入新数据,根据上一片的文章实例在其中添加新的控件: 编号TextBox(Name):sno 名字Te ...
- LINQ To SQL 处理 DateTime?
LINQ To SQL 处理 DateTime? 类型 例子: 搜索栏含有最后扫描时间的日期(DateTime?)与多个其他条件(String) 现在需要写一个查询 : 查询符合最后扫描的日期的查询 ...
- Linq to SQL各种参考
原文:https://www.cnblogs.com/lyj/archive/2008/01/23/1049686.htmlhttps://www.cnblogs.com/lyj/archive/20 ...
随机推荐
- laravel 添加自定义 Provider 配置之后不生效的问题
有可能是配置缓存导致的, 运行: php artisan config:clear 可清除配置缓存,配置缓存保存在 bootstrap/cache/config.php,可以直接去那文件夹看看是不是缓 ...
- Linux遇到的两个问题
编译C++的和编译C语言的命令不同. 编译C++,应当先安装g++. 然后可以用#g++ filename.cpp -o filename 进行编译 编译C语言是用#gcc filename.c -o ...
- CCD与CMOS的区别
我们在购买相机或是摄像机时,都会看到使用CMOS镜头或是CCD镜头,那么CCD与CMOS是什么意思呢,CCD与CMOS的区别是什么?首先,让我们了解CCD与CMOS的意思. CCDCCD使用一种高感光 ...
- Do the Untwist(模拟)
ZOJ Problem Set - 1006 Do the Untwist Time Limit: 2 Seconds Memory Limit: 65536 KB Cryptography ...
- Handlerbars基础笔记
此笔记摘抄于杨元的博客(http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) 引入 <script type=&qu ...
- bzoj 2213: [Poi2011]Difference
Description A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We w ...
- 【CodeForces】914 E. Palindromes in a Tree 点分治
[题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...
- 实现字符串检索strstr函数、字符串长度strlen函数、字符串拷贝strcpy函数
#include <stdio.h> #include <stdlib.h> #include <string.h> /* _Check_return_ _Ret_ ...
- NYOJ 739 笨蛋难题四 (数学)
题目链接 描述 这些日子笨蛋一直研究股票,经过调研,终于发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价,为了防止别人发现他的秘密.他决定对于这家公司的 股票最多买一次,现在笨蛋已经将 ...
- Linux下文本浏览器lynx
一般登录到Linux上的时候都是使用Shell登录上去的,但是如果这个时候我们有浏览网页的需求怎么办,比如我刚刚部署上去一个网站,但是我并不知道我有没有部署成功,而且只能在这一台Linux上能够访问到 ...