简体   繁体   English

如何从控件数据源获取对象上下文

[英]How to get objectcontext from a control datasource

I'm new in C# and Entity Framework. 我是C#和Entity Framework的新手。 Is there a way to get back the ObjectContext from control datasource ? 有没有办法从控制数据源中获取ObjectContext

I have three project , two for entity framework and the last one for window form application. 我有三个项目,两个用于实体框架,最后一个用于窗口表单应用程序。 The winform have two datagridview which each one from each EF project. Winform有两个datagridview,每个EF项目一个。

The issue are when I want to savechanges for datagridview A, I have to call MyEF_A.SaveChanges() and when I want to save datagridview B, I have to call MyEF_B.SaveChanges() . 问题是当我想保存datagridview A的更改时,我必须调用MyEF_A.SaveChanges()而当我想保存datagridview B时,必须调用MyEF_B.SaveChanges()

Is there a solution, so I can call SaveChanges() method base on Form.ActiveControl (datagridview A or datagridview B) tracing back from the control.datasource to ObjectContext ? 有没有解决方案,所以我可以基于Form.ActiveControl (datagridview A或datagridview B)调用从control.datasourceObjectContext SaveChanges()方法?

I'm afraid not. 恐怕不是。 The problem is that DataSource is of a type "object". 问题在于,数据源的类型为“对象”。 Even though you could cast it to its original type, you never use ObjectContext itself as a binding object, so you would get just the collection. 即使可以将其强制转换为原始类型,也绝不会将ObjectContext本身用作绑定对象,因此只获得集合。

I may be wrong though, please correct me someone if that's the case here. 不过我可能是错的,如果是这种情况,请纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM