ObjectContext是否会针对每个结果集进行更改?(Does the ObjectContext change for each result set?)
有许多示例显示了在使用Entity Framework时如何使用
ObjectContext
从多结果集存储过程中获取结果。
这些示例总是采用DbContext
,将其转换为IObjectContextAdapter
,并获取ObjectContext
,即使它们做了几行同样的事情。来自MSDN的示例(删除原始注释以简化和突出显示我的注释):
using (var db = new BloggingContext()) { db.Database.Initialize(force: false); var cmd = db.Database.Connection.CreateCommand(); cmd.CommandText = "[dbo].[GetAllBlogsAndPosts]"; try { db.Database.Connection.Open(); var reader = cmd.ExecuteReader(); var blogs = ((IObjectContextAdapter)db) .ObjectContext //First Time .Translate<Blog>(reader, "Blogs", MergeOption.AppendOnly); foreach (var item in blogs) { Console.WriteLine(item.Name); } reader.NextResult(); var posts = ((IObjectContextAdapter)db) .ObjectContext //Second Time .Translate<Post>(reader, "Posts", MergeOption.AppendOnly); foreach (var item in posts) { Console.WriteLine(item.Title); } } finally { db.Database.Connection.Close(); } }
我的存储过程返回许多结果集,所以我想通过将对
ObjectContext
的引用复制到本地变量来尽可能地简化代码,但我不确定这样做是否会导致问题。
我找到了一篇文章 ,表明“概念'DbContext'与'ObjectContext'相同。”
我没有发现任何明确指出对象实例的范围和生命周期。
ObjectContext
的文档有点缺乏细节。我更喜欢支持任何答案的官方文档的链接。
There are a number of examples showing how to use
ObjectContext
to get results from a multi-result set stored procedure when using Entity Framework.
These examples always take theDbContext
, cast it to aIObjectContextAdapter
, and get theObjectContext
, even if they did the same thing a few lines up.Example from MSDN (original comments removed to simplify and highlight my comments):
using (var db = new BloggingContext()) { db.Database.Initialize(force: false); var cmd = db.Database.Connection.CreateCommand(); cmd.CommandText = "[dbo].[GetAllBlogsAndPosts]"; try { db.Database.Connection.Open(); var reader = cmd.ExecuteReader(); var blogs = ((IObjectContextAdapter)db) .ObjectContext //First Time .Translate<Blog>(reader, "Blogs", MergeOption.AppendOnly); foreach (var item in blogs) { Console.WriteLine(item.Name); } reader.NextResult(); var posts = ((IObjectContextAdapter)db) .ObjectContext //Second Time .Translate<Post>(reader, "Posts", MergeOption.AppendOnly); foreach (var item in posts) { Console.WriteLine(item.Title); } } finally { db.Database.Connection.Close(); } }
My stored procedures return many result sets, so I would like to simplify the code as much as possible by copying the reference to
ObjectContext
to a local variable, but I am not sure if doing so will cause issues.
I did find an article which indicates that "Conceptually 'DbContext' is the same as 'ObjectContext'."
I have not found anything explicitly indicating the scope and lifetime of the object instance.
The documentation forObjectContext
is a bit lacking in details.I prefer a link to the official documentation supporting any answer.
原文:https://stackoverflow.com/questions/37036222
最满意答案
FacebookLogin
组件中存在一些错误。
- 该组件未连接到
redux
。 执行诸如export default connect()(FacebookLogin)
。 您需要从react-redux
导入connect
- 您需要调度
loginSuccesss
操作而不是直接调用它。 所以,你需要做类似this.props.dispatch(loginSuccess())
事情。HTH
There are a couple of errors in the
FacebookLogin
component.
- The component is not connected to
redux
. Do something likeexport default connect()(FacebookLogin)
. You will need to importconnect
fromreact-redux
- You need to dispatch the
loginSuccesss
action instead of directly calling it. So, you will need to do something likethis.props.dispatch(loginSuccess())
HTH
相关问答
更多-
FacebookLogin组件中存在一些错误。 该组件未连接到redux 。 执行诸如export default connect()(FacebookLogin) 。 您需要从react-redux导入connect 您需要调度loginSuccesss操作而不是直接调用它。 所以,你需要做类似this.props.dispatch(loginSuccess())事情。 HTH There are a couple of errors in the FacebookLogin component. The ...
-
Redux - 如何从另一个动作创建者中调用动作创建者?(Redux - how to call an action creator from inside another action creator?)[2021-10-22]
根据你的例子,你似乎已经在使用redux-thunk 。 如果是这种情况,您唯一忘记的就是调度您的动作创建者创建的动作: dispatch(foo(param)); According to your example, it seems that you're already using redux-thunk. If that's the case, the only thing that you're forgetting is to dispatch the action created by yo ... -
当组件只需要派遣一个动作创建者时,使用什么模式?(What pattern to use when a component only needs to dispatch an action creator?)[2021-11-20]
您可以只传递null ,而不是mapStateToProps函数。 有办法实现目标: 1. 注入dispatch功能,不听store export default connect()(Component); 然后在组件中 this.props.dispatch(actionCreator()); 2. 注入actions creators ,不要听store import * as actionCreators from './actionCreators' export default connec ... -
您确实需要mapDispatchToProps以允许组件首先启动操作。 它是同步还是异步并不重要。 但是,在异步操作中,通常会在您指出的时候解决承诺,这些组件不涉及处理结果。 相反,您可以在同一个动作创建器函数中派发一个动作(通常以_SUCCESS或_RECEIVE或_SAVE或其他结尾为结尾的动作 ) ,这个动作创建者函数中的reducer可以看到并更改redux状态。 这反过来将更改的道具通过mapStateToProps提供给组件。 只有这样 ,组件才会再次涉足。 You do need mapDis ...
-
store.dispatch(someThunk()).then()仅在thunk返回承诺时有效,并且您的thunk实际上并未返回承诺。 如果你只是在axios()前面放置一个return ,它应该可以工作。 store.dispatch(someThunk()).then() only works if the thunk returns a promise, and your thunk isn't actually returning a promise. If you just put a retu ...
-
我看到它的方式有三种选择: 通过在组件中封装isLoggedIn逻辑来防止调用toggleLoginModal 使用redux-thunk访问商店,并根据需要不dispatch任何操作 在你的reducer中处理这种逻辑,尽管我可能会三思而后行。 例子: 示例1:这假设您可以通过查看状态来判断用户是否已登录 class MyComp extends Component { render () { return ({!this.props.isLogged ...
多重派遣(multiple dispatch in redux action)[2024-02-09]
您可以单独导出这些功能,而不是将其包装在同一个对象下: export const getAllUser = () => dispatch => { ... } export const addUser = () => dispatch => { ... dispatch(getAllUser()); } 如果需要,您仍然可以将它们全部导入: import * as myActions from '...'; 或者你可以先声明getAllUser然后添加到myActions ,但是上面的解决 ...redux dispatch不起作用(redux dispatch does not work)[2023-02-08]
问题出在这一行, const reducer = (state = {}, action) => { 请注意,您已将状态初始化为空对象{} 。 因此,当您尝试传播state.numbers您会收到此错误,因为numbers未在初始状态中定义。 return { ...state, id:uuid(), numbers:[...state.numbers,newNumber] } 再现您的问题的最小示例, const bad = (state = {}) => { return { ...而不是将具有类型名称的部分推送到组件中 - 正如您所说,感觉就像在某种程度上打败了目的 - 您可以创建一个返回动作创建函数的函数(在此处插入Inception尾部噪声): const visibilityAction = type => (component, visibility) => ({ 'type': type, 'component': component, 'visibility': visibility }); export const updateMapVisib ...redux的store.dispatch()不是异步函数,因此不需要await它的返回。 它仍然有效,因为await非异步函数会将它们转换为立即解决的承诺。 关于分派多个操作,您需要手动分派所需的每个操作。 问题变成:为什么你需要派出这么多行动? 通常,对于ajax调用thunk,您将使用启动,成功和失败的操作,而不是更多。 export const fetchArticles = () => async (dispatch) => { try { dispatch(requestA ...相关文章
更多- 7月最新发布11.2.0.1.2 Patch set update
- How to set up Solr on Ubuntu 10.04 (or whatever)
- Memcached set 添加或更新数据命令
- hibernate有两个一对多的Set时怎么写hbm
- Memcached CAS(Check-And-Set)原子操作
- 题集
- [Hadoop] Error: JAVA_HOME is not set
- redis 字符串(String) SET 操作
- 怎么理解Set集合中的HashSet中的元素是无序的?
- Hadoop “Failed to set setXIncludeAware(true) for parser” 错误
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)