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 ...
随机推荐
- 洛谷P1012 拼数
题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时,4个整数7,13,4 ...
- C++ 局部静态变量,全局变量,全局静态变量,局部变量的区别和联系
C++变量根据定义位置的不同,具有不同的作用域,作用域可分为6种:全局作用域,局部作用域,语句作用域,类作用域,命名作用域和文件作用域. 从作用域看: 全局变量具有全局作用域.全局变量只需在一个源文件 ...
- C++调用函数模仿数字钟表
调用系统函数,可以得到系统时间 #include<iostream> #include<windows.h> #include<time.h> using name ...
- 「Python」5个开源项目
1-OpenAI universe Universe是一个能在世界上所有的游戏.网站和其他应用中,衡量和训练AI通用智能的软件平台. Universe,AI代理通过称为虚拟网络计算或VNC发送模拟的鼠 ...
- 阳/阴性预测值Positive/negative Predictive Value(推荐AA)
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
请检查你在web.xml中加载spring.xml文件的时候没有加载成功,看你的路径是否正确 <context-param> <param-name>contextConfi ...
- python---爬虫相关性能(各个异步模块的使用,和自定义异步IO模块)
一:线程池,进程池等相关文章了解 python---基础知识回顾(十)进程和线程(py2中自定义线程池和py3中的线程池使用) python---基础知识回顾(十)进程和线程(协程gevent:线程在 ...
- 深入浅出CSS(一):line-height与vertical-align的性质
[测试代码] <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- Codeforces 620C EDU C.Pearls in a Row ( set + greed )
C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...
- 实用的 Node.js 教程,工具和资源
这里分享一批实用的实用的 Node.js 教程,工具和资源. Node.js是一个建立在Chrome之上的JavaScript运行时平台,可方便地构建快速,可扩展的网络应用程序.Node.js使用事件 ...